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.

1
  • 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? Commented Jan 22, 2013 at 22:40

12 Answers 12

10

Ignore this answer as the project is no longer active

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

Sign up to request clarification or add additional context in comments.

3 Comments

@Mark: never heard about it. Thanks very much!
You may even draw graphs with perf4j, with the integrated servlet. It's quite good.
Link is broken :-(
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(); 

1 Comment

unfortunately I'm a netbeans user -:)
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.

1 Comment

yes, this is a very basilar implementation, essentialy I'm loooking for @Test(timeout=X, repeat=N) then display the average
7

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

2 Comments

nice to see it but it only encapsulates System.nanoTime/System.currentTimeMillis calls
its always easier to use than the default version ;)
4

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

Comments

2

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

1 Comment

Looks unmaintained, for anybody interested in this. Runs only on HTTP and JavaDoc is inaccessible.
1

Tried JPerf ?

2 Comments

Use google cache to see usage, Alternatively check the link to xjperf: code.google.com/p/xjperf
it seems a frontend for iperf, that is "a modern alternative for measuring maximum TCP and UDP bandwidth performance". It is right?
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.

Comments

1

Caliper

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

Comments

1

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

Comments

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.

Comments

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> 

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.