1

So I've written a rather silly program just to work with nanoTime a bit. I wanted to be able to check execution times of small bits of code so I figure nanoTime would be the best. I wanted to determine the average execution time of this short bit of code, so I put it inside a for loop. However, when inside the for loop, the average drops to about 6,000 nano seconds less. I know this isn't a huge difference on small code but I am curious why it would be any different for the same exact code? here are the two blocks that yield different times: this one is an average of about 8064 nano seconds:

  long start, end, totalTime;
  double milliseconds, seconds, minutes, hours, days, years;
  totalTime = 0;

  start = System.nanoTime();
  milliseconds = System.currentTimeMillis();
  seconds = milliseconds/1000;
  minutes = seconds/60;
  hours = minutes/60;
  days = hours/24;
  years = days/365;
  end = System.nanoTime();
  totalTime = end-start;

and this one is an average of about 2200 nano seconds:

  long start, end, totalTime;
  double milliseconds, seconds, minutes, hours, days, years;
  totalTime = 0;


  for(int i = 1; i < 11; i++){
     start = System.nanoTime();
     milliseconds = System.currentTimeMillis();
     seconds = milliseconds/1000;
     minutes = seconds/60;
     hours = minutes/60;
     days = hours/24;
     years = days/365;
     end = System.nanoTime();
     totalTime += end-start;
     System.out.println(end-start); //this was added to manually calc. the average to 
    //make sure the code was executing properly. does not effect execution time.
  }

and then to find the average you take totalTime*.1

Randi
  • 91
  • 1
  • 13
  • What do you mean by totalTime*.01? totalTime divided by 100?.. – alan7678 Apr 12 '16 at 22:20
  • 1
    To get started: http://shipilev.net/blog/2014/nanotrusting-nanotime/ Kidding ;-) this is rather involved. However, the question can likely be considered as a duplicate of http://stackoverflow.com/questions/16970191/why-does-my-algorithm-become-faster-after-having-executed-several-times-java (and probably dozens of others... they are still answered again and again...) – Marco13 Apr 12 '16 at 22:27
  • 1. You should not expect any accuracy from nanotime as Java do not guarantee it: "This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change.". 2. If you run the same code several times normally it will be executing faster with time due to JIT optimizations. – Yaroslav Rudykh Apr 12 '16 at 22:27
  • @alan7678 I just noticed I mistyped. It should be *.1. I do this instead of /10 because if I do /10 I lose the fractional component of the average. Not a significant loss but I like to see exact times. – Randi Apr 12 '16 at 22:29
  • @YaroslavRudykh I wasn't expecting each run to yield the exact same time. I do understand the runs will be slightly different each time, hence why I wanted to find the average run time- that being said, thanks for your input! – Randi Apr 12 '16 at 22:55

1 Answers1

3

This is exactly what you should expect from any Java program. The Java runtime, specifically the JIT compiler, will optimize code more heavily the more it gets run over the lifetime of the program. You should expect code to speed up after getting run multiple times.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413