Java High Speed ​​Stopwatch

I have this problem and I can’t figure out how to solve it.
I have to measure the time required to perform a certain function. Well, the stopwatch function measuring time in milliseconds was not good enough, so I used the measurement in nanoseconds.

The problem is that the function ends so quickly, even a stopwatch in nano seconds cannot measure it.

I know that the stopwatch works because I tried adding, for example, Thread.sleep(1) to my function (Thread.sleep in milliseconds), and I get my time, but without Thread.sleep my time is always 0. Any ideas ?

Here is my code:

 long startTimeLocalNS=0; long stopTimeLocalNS = 0; startTimeLocalNS = System.nanoTime(); if (something) { /*my Code;*/ } stopTimeLocalNS = System.nanoTime(); disconnectTime = (stopTimeLocalNS - startTimeLocalNS); 
+4
source share
7 answers

How to run a function for several thousand (or millions) times in a loop, measuring the total time and dividing it by the number of iterations?

But be careful with numerous errors when writing microobjects .

+7
source

You can do something like this:

 long startTimeLocalNS=0; long stopTimeLocalNS = 0; int count = 10000; startTimeLocalNS = System.nanoTime(); if (something) { while(count-- != 0) { /*my Code;*/ } } stopTimeLocalNS = System.nanoTime(); disconnectTime = (stopTimeLocalNS - startTimeLocalNS) / count; 
+1
source

You can scroll the measured code 1000 times, and then divide the nanoTime difference by 1000.

+1
source

Are you profiling code? If so, the profiles available in the market will work much better.

If you just need to measure a very short period of time, you have several options:

  • System.currentTimeMillis() - on my computer it has a resolution of 60 Hz, which gives about 16 ms of granularity
  • System.nanoTime() - suppose to be more precise, but I heard that in the era of multi-core processors this is not the fastest solution
  • JNI - measure time in C, but the cost of calling JNI will be significant.
+1
source

If you want to compare your function, the easiest way is to run it several times within the same dimension. Then divide the time by the number of calls.

0
source

You will get a completely different answer depending on whether the JVM warms up or not. For very short sequences that you will call many times. You need time to repeat them.

0
source

The answer is from the left field: as Peter Lowry claims, most systems should return a number greater than zero for the result when comparing two calls with nanoTime() . However, are you absolutely sure you are comparing the correct values? Could you accidentally do this:

 long startTime = System.nanoTime(); if (something) { /*my Code;*/ } long stopTime = System.nanoTime(); disconnectTime = stopTime - stopTime; //note the error - you are subtracting stoptime from itself 

So my question is: is there an error in your code that you did not reproduce in the above code example?

Second question: are you sure something==true ?

0
source

Source: https://habr.com/ru/post/1341290/


All Articles