0

I'm creating a program where everytime it goes to a function it gets the execution time of that function, but the problem is that when once it enters the first function the next functions will get 0 or 1 ms of execution time:

function1: 
long start = System.currentTimeMillis();
//
//
//some code
//
//
//
long end = System.currentTimeMillis();
System.out.println("function1 took: " + (end - start) + "ms"); 

function2: 
long start = System.currentTimeMillis();
//
//
//some code
//
//
//
long end = System.currentTimeMillis();
System.out.println("function2 took: " + (end - start) + "ms"); 

function3: 
long start = System.currentTimeMillis();
//
//
//some code
//
//
//
long end = System.currentTimeMillis();
System.out.println("function3 took: " + (end - start) + "ms"); 

the output is:

function1 took: 49ms

function2 took: 1ms

function3 took: 0ms

As you can see, after the first funtion, it seems that the time is not calcualted correctly for the following functions. hope you can answer my doubt.

Zeta Zav
  • 5
  • 3
  • 2
    Why do you want to time the method? If you are doing benchmarking, then consider using some sophisticated tools like [JMH](https://openjdk.java.net/projects/code-tools/jmh/). Try using `System.nanoTime()` instead – Lavish Kothari Feb 25 '19 at 04:52
  • 1
    `it seems that the time is not calcualted correctly` or it just takes less than a millisecond... – tkausl Feb 25 '19 at 04:53
  • 2
    It's possible that some classes are loaded / initialised in the first function call, and are just reused in subsequent calls. When measuring execution time, you need to be very careful that you understand exactly the scope of what you are measuring. – Jason Feb 25 '19 at 04:54
  • 1
    Function 2 and function 3 might be taking less time. Just check your code. – Madhukar Hebbar Feb 25 '19 at 04:55
  • Try `System.nanoTime()` to see time smaller than a millisecond. – Kartik Feb 25 '19 at 04:58
  • Possible duplicate of [How do I time a method's execution in Java?](https://stackoverflow.com/questions/180158/how-do-i-time-a-methods-execution-in-java) – Jerry Chin Feb 25 '19 at 05:38
  • function 2 and 3 don't take less time, if i switch them, for example, first exectue function 2, then 3 and then 1, the output would be something like: function2 took : 25ms, function3 took: 0ms, function1 took: 0ms, so the first function to execute will get the real time of execution. – Zeta Zav Feb 25 '19 at 06:09
  • The granularity of `System.currentTimeMillis()` depends on the precision of a timer, which might only have a granularity of 10 (or more) milliseconds. So if the timed thing takes less than that, it might appear to take only 0 ms. Related: https://stackoverflow.com/questions/7859019/system-currenttimemillis-is-not-accurate-on-windows-xp – Mark Rotteveel Feb 25 '19 at 10:53

1 Answers1

1

Although it depends on your method and implementation, i think ms is not accurate enough to give you that time.

You can use link How do I time a method's execution in Java? for more accurate time.

Also you can put each method in a separate thread and run concurrently if they are independent for better time in your overal execution.

Amir Hedieh
  • 1,120
  • 1
  • 14
  • 28