How can I calculate the time it takes to execute a function in Java?

I need to measure the time it takes to complete a function in Java. How can i do this?

Note:

I want to measure the consumption of a function , not a complete program.

+42
java profiling
Mar 28 '09 at 10:35
source share
10 answers
long start = System.nanoTime(); methodToBeTimed(); long elapsedTime = System.nanoTime() - start; 
+75
Mar 28 '09 at 10:43
source share

Here's how to calculate elapsed time.

 // Get current time long start = System.currentTimeMillis(); // Do something ... // Get elapsed time in milliseconds long elapsedTimeMillis = System.currentTimeMillis()-start; // Get elapsed time in seconds float elapsedTimeSec = elapsedTimeMillis/1000F; // Get elapsed time in minutes float elapsedTimeMin = elapsedTimeMillis/(60*1000F); // Get elapsed time in hours float elapsedTimeHour = elapsedTimeMillis/(60*60*1000F); // Get elapsed time in days float elapsedTimeDay = elapsedTimeMillis/(24*60*60*1000F); 
+38
Mar 28 '09 at 10:49
source share
+8
Mar 28 '09 at 12:23
source share

If you are using Guava, consider using Stopwatch , for example:

 final Stopwatch sw = Stopwatch.createStarted(); methodToBeTimed(); final long elapsedMillis = sw.elapsed(TimeUnit.MILLISECONDS); 
+8
Jun 10 '13 at 9:45
source share

A profiler is the right answer if you have several features.

Another problem that I see with all the offers that have been given so far is that they work fine for one function, but your code will be dotted with timings that you cannot turn off.

If you know how to do aspect-oriented programming, this is a good way to keep the synchronization code in one place and apply it declaratively. If you use something like Log4J to output values, you can turn it off or on. This is a bad profiler.

Take a look at AspectJ or Spring AOP.

+6
Mar 28 '09 at 14:52
source share

All the code snippets above measure the approximate time elapsed from the moment the method is called until the method returns / throws an exception. Such methods are not intended for thread scheduling; they are suspended due to GC, etc.

Yes, some profilers will do a reasonable job.

If you are using Java 1.6, you can use JMX-based VM management and monitoring support. For example, you can find ThreadMXBean.getCurrentThreadCpuTime () . Calculating the difference of this value before and after the method call will give you:

"... the total processor time for the current thread in nanoseconds. The return value is nanosecond accuracy, but not necessarily the nanosecond accuracy. If the implementation differs in user mode time and system mode time, the return CPU time is the amount of time that the current thread has completed in the user mode or system mode. "

If your method spawns workflows, then your calculations should be more complex; -)

In general, I recommend using the java.lang.mangement package .

+5
Mar 28 '09 at 16:10
source share

Use either System.currentTimeMillis () or System.nanoTime ():

 int someMethod() { long tm = System.nanoTime(); try { ... } finally { tm = System.nanoTime()-tm; System.out.println("time spent in someMethod(): " + tm + "ns"); } } 
+3
Mar 28 '09 at 10:46
source share

System.nanoTime should be used to accurately measure the delta between two moments for microobjects.

 public class CountTime { private static void walk() { for (int i = 0; i < Integer.MAX_VALUE; i++) ; } public static void main(String[] args) { long t0 = System.nanoTime(); walk(); long t1 = System.nanoTime(); System.out.println("Elapsed time =" + (t1 - t0) + " nanoseconds"); } } 

System.currentTimeMillis returns the current time in milliseconds. You can use this to get the current time. This can be useful for older virtual machines or for longer processes.

+2
Mar 28 '09 at 10:45
source share

this analog stopwatch is another option for us too ... Check out Java2s.com here.

0
Aug 21 2018-11-21T00:
source share

If you want to get the current time, use java.util.Date .

-2
Mar 28 '09 at 10:38
source share



All Articles