I have a simple below program which iterates through an array
Integer [] intArray = new Integer[20000];
int index=0;
for(int i=10000; i>=0; i--){
intArray[index]=i;
index++;
}
long startTime = System.currentTimeMillis();
for(Integer t : intArray){
System.out.println(t);
}
long endTime = System.currentTimeMillis();
long consumedTime = endTime-startTime;
System.out.println("Consumed time "+ consumedTime);
I always get different values of consumed time like 743, 790, 738, 825, 678.
Why time taken by for loop is always different for each execution.
Note I am running this code inside a main method. My OS is Ubuntu and processor is 32 bit.
Because your program isn't the only thing running on the machine. The OS itself, all the other apps, etc...they take CPU time too -- and not always the exact same amount.
There is no specific time Java
programs will take. It depends what all is running on the machine. Also since you are using Integer
it takes more time. If you just native differences will likely be less.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With