How do I get a method's execution time? Is there a Timer utility class for things like timing how long a task takes, etc?
Most of the searches on Google return results for timers that schedule threads and tasks, which is not what I want.
How do I get a method's execution time? Is there a Timer utility class for things like timing how long a task takes, etc?
Most of the searches on Google return results for timers that schedule threads and tasks, which is not what I want.
If you want wall-clock time
With Java 8 you can do also something like this with every normal methods:
with TimeIt like:
With this methode you can make easy time measurement anywhere in your code without breaking it. In this simple example i just print the time. May you add a Switch for TimeIt, e.g. to only print the time in DebugMode or something.
If you are working with Function you can do somthing like this:
I modified the code from correct answer to get result in seconds:
You can use Perf4j. Very cool utility. Usage is simple
More information can be found in Developer GuideEdit: Project seems dead
You can try this way if just want know the time.
In Java 8 a new class named
Instant
is introduced. As per doc:This can be used as:
It prints
PT7.001S
.