Is there a command in java for measuring runtime?

Is there a command in java for measuring runtime?

Sort of

System.out.println(execution.time); 

at the end of the code.

+4
source share
9 answers

Here is a complete and slightly modified example of how you could do this:

 public class ExecutionTimer { private long start; private long end; public ExecutionTimer() { reset(); start = System.currentTimeMillis(); } public void end() { end = System.currentTimeMillis(); } public long duration(){ return (end-start); } public void reset() { start = 0; end = 0; } public static void main(String s[]) { // simple example ExecutionTimer t = new ExecutionTimer(); for (int i = 0; i < 80; i++){ System.out.print("."); } t.end(); System.out.println("\n" + t.duration() + " ms"); } } 
+7
source

You can easily implement this yourself using System.currentTimeMillis() :

 final long start = System.currentTimeMillis(); executeLongRunningTask(); final long durationInMilliseconds = System.currentTimeMillis()-start; System.out.println("executeLongRunningTask() took " + durationInMilliseconds + "ms."); 

Alternatively (especially if your task has not been completed for so long), you can use System.nanoTime() . Note that contrary to how currentTimeMillis() works, the value returned by nanoTime() is not relative to a specific time. This means that nanoTime() can only be used to measure time intervals and cannot be used to identify a specific point in time.

+3
source

You can run the profiler or use the difference of two calls on System.currentTimeMillis()

Like this:

 long start = System.currentTimeMillis(); .... doSomething(); .... long end = System.currentTimeMillis(); System.out.println("Execution time was "+(end-start)+" ms."); 
+2
source

The easiest way is to use System.currentTimeMillis () before and after code execution. Joda-Time has more complex versions: http://joda-time.sourceforge.net/

+2
source

If you want to know more about what you are measuring, I highly recommend that you use JMX, especially ThreadMXBean: http://download.oracle.com/javase/6/docs/api/java/lang/management/ThreadMXBean. html

Code example:

 ThreadMXBean bean = ManagementFactory.getThreadMXBean( ); if (bean.isCurrentThreadCpuTimeSupported()) { long cpuTime = bean.getCurrentThreadCpuTime( ); } long userTime = bean.getCurrentThreadUserTime( ); 

You can find a complete explanation here with code examples: http://nadeausoftware.com/articles/2008/03/java_tip_how_get_cpu_and_user_time_benchmarking

+1
source

Use ThreadMXBean for more detailed time:

 public class Timer { static { // needed to request 1ms timer interrupt period // http://discuss.joelonsoftware.com/default.asp?joel.3.642646.9 Thread thread = new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(Integer.MAX_VALUE); (Windows NT) } catch (InterruptedException ignored) { } } }); thread.setName("Timer"); thread.setDaemon(true); thread.start(); } private final ThreadMXBean threadMX = ManagementFactory.getThreadMXBean(); private final long elapsedStart; private final long cpuStart; private final long userStart; public Timer() { cpuStart = threadMX.getCurrentThreadCpuTime(); userStart = threadMX.getCurrentThreadUserTime(); elapsedStart = System.nanoTime(); } public void times() { long elapsed = elapsedStart - System.nanoTime(); long cpu = cpuStart - threadMX.getCurrentThreadCpuTime(); long user = userStart - threadMX.getCurrentThreadUserTime(); System.out.printf("elapsed=%-8.3f cpu=%-8.3f user=%-8.3f [seconds]", elapsed/1.0e9, cpu/1.0e9, user/1.0e9); } } 
+1
source

The Apache Commons library has a StopWatch class and Spring also has a StopWatch .

+1
source

You can create an abstraction of the time control, which takes as an argument the action that needs to be performed, and measures and prints the time it takes to complete it.

Code:

 interface Action<A> { public A perform(); } class Timer { public static <A> A time(final String description, final Action<A> action) { final long start = System.nanoTime(); final A result = action.perform(); final long end = System.nanoTime(); System.out.println(description + " - Time elapsed: " + (end - start) +"ns"); return result; } } class Main { public static void main(final String[] args) { final int factorialOf5 = Timer.time("Calculating factorial of 5", new Action<Integer>() { public Integer perform() { int result = 1; for(int i = 2; i <= 5; i++) { result *= i; } return result; } } ); System.out.println("Result: " + factorialOf5); } } // Output: // Calculating factorial of 5 - Time elapsed: 782052ns // Result: 120 
0
source

I liked the example of the RoflcoptrException class. I rewrote it first:

 public class ExecutionTimer { private long start; public ExecutionTimer() { restart(); } public void restart() { start = System.currentTimeMillis(); } public long time(){ long end = System.currentTimeMillis(); return (end-start); } public String toString() { return "Time="+time()+" ms"; } } 
0
source

Source: https://habr.com/ru/post/1345697/


All Articles