CRT_flow
CRT_flow

Reputation: 117

Java high precision stopwatch

I have this problem, and I can't get any idea how to solve it.
I have to measure the time needed to perform certain function. Okay, stopwatch function that measure time in milliseconds wasn't good enough, so I used measurement in nano seconds.

The problem is, function ends so fast, even stopwatch in nano seconds can't measure it.

I know that stopwatch works, because I tried to put for example Thread.sleep(1) in my function (Thread.sleep is 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);

Upvotes: 4

Views: 3214

Answers (7)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340763

Are you profiling some code? If so, profiles available on the market will do a much better job.

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

  • System.currentTimeMillis() - on my computer it has a 60Hz resolution, which gives about 16ms granularity
  • System.nanoTime() - suppose to be more accurate, but I heard that in the age of multi-core CPUs it's not the fastest solution
  • JNI - measure the time in C, but the cost of JNI invocation will be significant.

Upvotes: 1

Rich
Rich

Reputation: 15767

An answer out of left field: as Peter Lawrey states, most systems should be returning a number greater than zero for a result when comparing two calls to nanoTime(). However, are you absolutely certain that you are comparing the right values? Could you accidentally be doing 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 a bug in your code that you've not reproduced in the sample code above?

Second question, are you certain that something==true?

Upvotes: 0

Bernd Elkemann
Bernd Elkemann

Reputation: 23550

You can loop through the to-be-measured code 1000 times, then divide the nanoTime-difference by 1000.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533560

You will get a very different answer depending on whether the JVM is warmed up or not. For very short sequences which you will be calling many times. You should time how long it takes to repeat them.

Upvotes: 0

Andrey
Andrey

Reputation: 60065

If you want to benchmark your function easiest way is to run it several times inside one measurement. Then divide time by amount of calls.

Upvotes: 0

adarshr
adarshr

Reputation: 62593

You could 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;

Upvotes: 1

Michael Borgwardt
Michael Borgwardt

Reputation: 346327

How about running the function a few thousand (or million) times in a loop, measuring the total time and dividing it by the number of iterations?

But beware the many pitfalls in writing microbenchmarks.

Upvotes: 7

Related Questions