@ Ben S answer is in place.
However, it should be noted that the approach of inserting time measurement instructions in your code does not scale:
- This turns your code into a mess.
- This makes your application run slower. Those calls to
System.nanoTime() do not come for free! - He introduces the possibility of errors.
If your real goal is to try to find out why your application is slow, so you decide what to optimize, then using the Java profiler is the best solution. This has the advantage that you need to make ZERO changes to the source code. (Of course, profiling does not give you the exact time spent in certain sections. Rather, it gives you time proportions ... which is much more useful for deciding where to optimize.)
Stephen c
source share