0

I created a presentation rendering framework using java. In need to calculate how much time taken by the user for the each frame in the presentation. So is there is any api or framework in java which looks like a stopwatch.

Anish Antony
  • 875
  • 3
  • 17
  • 35

2 Answers2

3

For measurement you could use:

System.nanoTime();

Returns the current value of the most precise available system timer, in nanoseconds.

This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary time.

for example from oracle documentation:

long startTime = System.nanoTime();
// ... the code being measured ...
long estimatedTime = System.nanoTime() - startTime;
Salah
  • 8,567
  • 3
  • 26
  • 43
1

Use System.nanoTime() which exists precisely for this purpose. If you want extra goodies like easy manipulation of elapsed time, support for laps, then you can have a look at the com.google.common.base.StopWatch in the Guava library.

Example:

   Stopwatch stopwatch = Stopwatch.createStarted();
   doSomething();
   stopwatch.stop(); // optional

   long millis = stopwatch.elapsed(MILLISECONDS);

   log.info("time: " + stopwatch); // formatted string like "12.3 ms"

Guava also includes other goodies like collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O, and so forth.