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
Why Am I Getting a Noclassdeffounderror in Java
How to Create an Executable Jar With Dependencies Using Maven
Java: Splitting a Comma-Separated String But Ignoring Commas in Quotes
How to Read Xml Using Xpath in Java
Setting the Default Java Character Encoding
Sort Arraylist of Custom Objects by Property
What Is an Efficient Way to Implement a Singleton Pattern in Java
How Does the Java 'For Each' Loop Work
How to Load Jar Files Dynamically At Runtime
Com.MySQL.Jdbc.Exceptions.Jdbc4.Communicationsexception: Communications Link Failure
How to Declare and Initialize an Array in Java
Can You Find All Classes in a Package Using Reflection
What Causes and What Are the Differences Between Noclassdeffounderror and Classnotfoundexception
Cannot Create or Edit Android Virtual Devices (Avd) from Eclipse, Adt 22.6
Non-Static Variable Cannot Be Referenced from a Static Context