3

I want to compute the execution time of my Java Program and I am confused how I should go about it. I know that time elapsed can be computed using System.nanoTime() & System.currentTimeInMillis(), however this approach does not take into consideration the underlying scheduling mechanism of the Operating System.

For example : Say there are 3 processes running at the same time (lets assume its a single core system)

Process A

Process B (My Java Code - running in JVM Process)

Process C

Now if Process B starts executing at 11:00 AM and completes execution at 11:02 AM, System.nanoTime() & System.currentTimeInMillis() will report the time taken as 2 minutes. However its possible that Process B might not be running for the duration of entire 2 minutes, because the OS will schedule the other 2 Process and they would be running for some amount of time in the 2 minute interval. So in essence the java Program would run for less than 2 minutes.

Is there some way to pinpoint the exact time that was taken by a Java Program itself to complete execution? Note :

  1. We can run the code multiple times and take average time to minimize the effects of such cases, still it would be good to know if there is some reliable way to compute the execution time

  2. Lets assume that the portion of code that we need to time cannot be broken into smaller chunks and takes a large amount of time to process.

ballesta25
  • 93
  • 1
  • 5
Kakarot
  • 4,252
  • 2
  • 16
  • 18
  • 1
    use a profiler to check the time – stinepike Feb 23 '14 at 06:15
  • The answer is here - Seems like a duplicate of the SO question - http://stackoverflow.com/questions/180158/how-do-i-time-a-methods-execution-in-java – spiderman Feb 23 '14 at 06:16
  • The answers there focus more on using nanotTime()/currentTimeInMillis() for computing the execution time, but it does not take into consideration the OS scheduling time. – Kakarot Feb 23 '14 at 06:18
  • I used to put `date` `time` on my shell PS1 so that I know how long every command runs – zinking Feb 23 '14 at 08:25
  • Try this mechanism: http://stackoverflow.com/questions/3382954/measure-execution-time-for-a-java-method – yegor256 Jan 19 '15 at 08:16

2 Answers2

5

Just in case you are using Java 8 then it has Instant class which represents an instantaneous point on the time-line. This class models a single instantaneous point on the time-line. This might be used to record event time-stamps in the application as:

    Instant start = Instant.now();
    try {
        Thread.sleep(7000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    Instant end = Instant.now();
    System.out.println(Duration.between(start, end));
}

And it prints: PT7.001S

akhil_mittal
  • 23,309
  • 7
  • 96
  • 95
4

The old fashion way is... Break it into methods

long startTime = System.nanoTime();
methodToTime();
long endTime = System.nanoTime();

long duration = endTime - startTime;
Moonhead
  • 1,570
  • 8
  • 17
  • This is a approach that we usually take break into small functions so and time them to avoid the effects of scheduling. But if there is something that cant be broken and it takes significant time to compute then we end up facing the issue of timing it correctly. – Kakarot Feb 23 '14 at 06:12
  • I'm sure you if you added all those calculation methods up that you would receive approximately the same time as if it were one. If it's profiling that you need to do on your program that I would suggest using NetBean's profiling tool, but for overall execution time System.currentTimeMillis(); should be enough to get what information you wanted – Moonhead Feb 23 '14 at 06:14