How long does it take to iterate through a long loop?
- by Carl Rosenberger
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");
}
}