optimization - Varying execution time of for loop in Java -


i newbie in java. have done following coding.

class timecomplex{     public static void main(string []args){         long starttime, stoptime, elapsedtime;          //first call         starttime = system.currenttimemillis();         system.out.println("\nstart time : " + starttime + "\n");         calcforloop();         stoptime = system.currenttimemillis();         system.out.println("stop time : " + stoptime + "\n");         elapsedtime = stoptime - starttime;         system.out.println("\t1st loop execution time : " + elapsedtime+ "\n");          //second call         starttime = system.currenttimemillis();         system.out.println("start time : " + starttime + "\n");         calcforloop();         stoptime = system.currenttimemillis();         system.out.println("stop time : " + stoptime + "\n");         elapsedtime = stoptime - starttime;         system.out.println("\t2nd loop execution time : " + elapsedtime + "\n");          //third call         starttime = system.currenttimemillis();         system.out.println("start time : " + starttime + "\n");         calcforloop();         stoptime = system.currenttimemillis();         system.out.println("stop time : " + stoptime + "\n");         elapsedtime = stoptime - starttime;         system.out.println("\t3rd loop execution time : " + elapsedtime + "\n");     }      static void calcforloop(){         for(long = 12_85_47_75_807l; > 0; i--);     } } 

the code runs for-loop long period of time increase execution time of program. when calcforloop() called first time execution time of program maximum when same method called second time program takes lesser time first call , third call method calcforloop() less or equal second execution time. ran program 5-6 times , got same pattern of execution time. this output of program.

my question why happen when code execute remains same 3 time. there code optimization takes place compiler or dependent on operating system environment. sort of optimization done compiler when there repeated execution of same block of code in case?

the jit (just in time) compiler compiles code jvm detects being executed lot, accounts increase in performance. there may brief reduction in performance while such compilation taking place.

it usual allow effect in performance testing - execute code few thousand times, start benchmark.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -