How to Time a Method'S Execution in Java

How do I time a method's execution in Java?

There is always the old-fashioned way:

long startTime = System.nanoTime();
methodToTime();
long endTime = System.nanoTime();

long duration = (endTime - startTime); //divide by 1000000 to get milliseconds.

What's a clean way to time code execution in Java?

You solution is just fine.

A less expressive way would be to wrap your code to be timed in a lambda.

public void timeCode(Runnable code) {
...
try {
code.run();
} catch ...
}
...
}

timeCode(() -> { ...code to time... });

You would probably like to catch the checked exceptions and pass them to some runtime exception or whatever.

How do I time a method's execution in Java?

There is always the old-fashioned way:

long startTime = System.nanoTime();
methodToTime();
long endTime = System.nanoTime();

long duration = (endTime - startTime); //divide by 1000000 to get milliseconds.

Method's execution time in java

Why don't you just use a profiler, like YourKit or JVisualVM. JVisualVM comes with the JDK. If you really want to do this yourself, use java.lang.instrument and ASM to write your own agent. It's simple to add logic to the prologue and epilogue of a Java method using this approach.

Here's a link to get you started.

How to measure the execution time with Java

In order for this to work, this line:

long start = System.currentTimeMillis(); 

should be before this line:

ob.sort(nums, 0, nums.length-1);

Also, keep in mind this is measuring Time and not Time Complexity

How do you stop a java method execution with a timer?

You will need to do more than add throws InterruptedException to all of those ‘submethods’ (and your own methods). The body of each of those methods must be altered to properly respond to interrupts.

It is not possible to arbitrarily stop running code. Interrupts are cooperative—they only mean something if the thread being interrupted pays attention to them.

Your run() method does this properly: by placing the entire loop inside a try/catch, any InterruptedException will cause the loop to terminate and thus the thread will terminate.

But the methods it calls must do the same thing. Your run method calls executeExperiment, which does this:

String[] files = getFiles(carpeta);

I don’t know how long that method takes, but if it takes any significant amount of time at all (more than a fraction of a second), it needs to be capable of throwing InterruptedException in the middle of the file reading.

executeExperiment also calls executeCase, which calls the ‘submethods’ readDataToGraph, ExactSolution, and addNewCase. As above, each of those methods which takes more than a fraction of a second needs to respond to an interrupt by throw InterruptedException. So, I’m afraid you will need to modify them.

An example would be:

private Graph readDataToGraph(String filename)
throws InterruptedException {

Graph graph = new Graph();
try (BufferedReader reader = Files.newBufferedReader(Path.of(filename))) {
String line;
while ((line = reader.readLine()) != null) {
graph.addData(convertDataToGraphEntry(line));

if (Thread.interrupted()) {
throw new InterruptedException();
}
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}

}


Related Topics



Leave a reply



Submit