4

Python has a timeit module could use it to time code execution, e.g.

>>> timeit.timeit(lambda:sleep(0.1), number=10)
1.02257220899628

Does in java have similar tool could time code execution as timeit, e.g.

@Timeit(number=10)
@Test
public void foo(){
    service.foo();
}
zhuguowei
  • 8,401
  • 16
  • 70
  • 106
  • 1
    For short running parts (i.e. microbenchmarks), there is the [Java Microbenchmark Harness](http://openjdk.java.net/projects/code-tools/jmh/) ([Tutorial by Jenkov](http://tutorials.jenkov.com/java-performance/jmh.html)) – Turing85 May 04 '18 at 14:43
  • Since you seem to want to use it on a test: JUnit has a [timeout annotation](https://junit.org/junit4/javadoc/4.12/org/junit/rules/Timeout.html) – Robert May 04 '18 at 14:48

2 Answers2

0

Not really. You can create a wrapper class using System.nanoTime().

Also, there is a similar Stack overflow question with answers posted here: How do I time a method's execution in Java?

Also of use: a very rudimentary implementation: Timeit.java

0

Metrics could provide what you're looking for. But it might be a bit overkill in most cases.

Alexandre GC
  • 553
  • 3
  • 8