Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Java

Idiom #125 Measure function call duration

measure the duration t, in nanoseconds, of a call to the function foo. Print this duration.

import static java.lang.System.nanoTime;
import static java.lang.System.out;
long t = nanoTime();
foo();
out.println(nanoTime() - t);
long t = System.nanoTime();
foo();
t = System.nanoTime() - t;
System.out.println(t + "ns");
(time (foo))

time prints time elapsed, but in ms

New implementation...
< >
JPSII