22

I frequent wrap code in a System.nanoTime() pair in order to timing it. Something like:

long start = System.nanoTime();    
methodToBeTimed();
long elapsedTime = System.nanoTime() - start;

There is any good timing library that helps with this problem? Also homegrown code will be accepted.

NB

A profiler is not a solution here, since I want to enforce some time constraints in my unit tests, so I want to timing methods programmatically.

dfa
  • 114,442
  • 31
  • 189
  • 228
  • I don't understand what the "problem" is that you are trying to solve. You have one line at the top, and one more line at the bottom to give you elapsed time. And you have one variable to hold it. You could wrap this in a class, or use the Stopwatch class, but you won't really reduce the complexity of the code in any way: you still will need one line at the top, and one line at the bottom. Is it that you want help recording and tracking a large number of such timings? – AgilePro Jan 22 '13 at 22:40

12 Answers12

10

Ignore this answer as the project is no longer active

I haven't used it but I came across perf4j recently.

Mark
  • 28,783
  • 8
  • 63
  • 92
9

Not a direct answer to your question, but I am also often using this tip to time my code and just wrote the following simple Eclipse -> Surround With template:

long startTime = System.currentTimeMillis();
${line_selection}${cursor}
long totalTime = System.currentTimeMillis() - startTime;
System.out.println("Total time = " + totalTime);
System.out.println();
Manuel Selva
  • 18,554
  • 22
  • 89
  • 134
8

JUnit 4 got a built-in timing-contraint functionality.

@Test(timeout=X)

should do the trick. X is the maximum number of milliseconds the method is allowed to run.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Philipp
  • 7,199
  • 1
  • 22
  • 16
  • yes, this is a very basilar implementation, essentialy I'm loooking for @Test(timeout=X, repeat=N) then display the average – dfa Aug 06 '09 at 09:04
7

there is StopWatch from commons-lang, it also allows you to split timer.

user802421
  • 7,465
  • 5
  • 40
  • 63
IAdapter
  • 62,595
  • 73
  • 179
  • 242
4

If you're using Spring you already have nice class called StopWatch in your classpath for this propose.

Malax
  • 9,436
  • 9
  • 48
  • 64
2

JETM is a good library for doing this. It can also provide with mins, maxs and averages, plus can generate informative graphs.

Kai
  • 38,985
  • 14
  • 88
  • 103
ahanin
  • 892
  • 4
  • 8
1

Tried JPerf ?

techzen
  • 2,895
  • 2
  • 22
  • 22
  • Use google cache to see usage, Alternatively check the link to xjperf: http://code.google.com/p/xjperf/ – techzen Aug 06 '09 at 06:22
  • it seems a frontend for iperf, that is "a modern alternative for measuring maximum TCP and UDP bandwidth performance". It is right? – dfa Aug 06 '09 at 06:25
1

What kind of help are you looking for with this problem? You have the basics in place. You get the elapsed time in Nanoseconds, accurate to whatever resolution the underlying OS/Hardware is capable of.

Also... and I know you said no profilers... but I have had outstanding experience with YourKit. It provides an API that you can use to control profiling from the outside. Depending on what your exact problem is, this one might be worth having a look at.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
1

Something new on the market is JMH. It's made under the umbrella of the openjdk project.

keiki
  • 3,260
  • 3
  • 30
  • 38
1

I've just started using Java Simon (formerly on Google Code), and it seems awesome! A simple set of stopwatches and counters with few dependencies.

Garret Wilson
  • 18,219
  • 30
  • 144
  • 272
1

Caliper

And in addition to reading the wikis on that site, i recommend reading:

nicerobot
  • 9,145
  • 6
  • 42
  • 44
0

A simple wrapper for commons-lang StopWatch which take into account multi threading environment, when stop is not invoked leap stat is not counted and 'failed leaps count' incremented (you may not include stop in finally block if the behavior is desired), exposes live statistic via JMX and deliver consolidated serializable result over the network. Example usage:

private PerfStat methodPerf = new PerfStat("method.under.test");
...
public void myMethod() {
    methodPerf.start();
    ...
    long timeNs = methodPerf.stop();
}

dependency

<dependency>
    <groupId>org.droolsassert</groupId>
    <artifactId>da-utils</artifactId>
</dependency>
Mike
  • 20,010
  • 25
  • 97
  • 140
  • 1
    I don't think your implementation for `minLapTime` would work, since it is initialized with the lowest possible value: 0. It should probably be initialized with (and reset to) `Long.MAX_VALUE`. – martin May 29 '13 at 09:57