How long does it take to iterate through a long loop?

Posted by Carl Rosenberger on Stack Overflow See other posts from Stack Overflow or by Carl Rosenberger
Published on 2010-05-25T12:51:49Z Indexed on 2010/05/25 13:01 UTC
Read the original article Hit count: 386

Filed under:

On a modern 64-Bit machine, how long do you think it takes to iterate through all the positive long numbers?

Below is a code snippet in Java to demonstrate the idea.

Without running the code yourself, how long do you think this code is going to run?

How long will similar code run in other programming languages?

public class LongLoop {

    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        for (long i = 0; i < Long.MAX_VALUE; i++) {
            // do nothing, just loop
        }
        long stopTime = System.currentTimeMillis();
        long duration = stopTime - startTime;
        System.out.println("Time taken: " + duration + " milliseconds");
    }
}

© Stack Overflow or respective owner

Related posts about long