public class Test {
public static void main(String[] args) {
int x = 150_000;
long start = System.currentTimeMillis();
for(int i = 0; i < x; i++) {
f1(i);
}
long end = System.currentTimeMillis();
System.out.println((end - start) / 1000.0);
}
private static long f1(int n) {
long x = 1;
for(int i = 0; i < n; i++) {
x = x + x;
}
return x;
}
}
Can someone explain why setting x to 150_000
or 4_000_000
or even 2_000_000_000
doesn't change execution time of this loop?
f1
is returning some value, but that value is never used, and no other variables are being modified during the method. It would sense (to me at least) that the compiler completely ignores the call.i
to be of typelong
.