Question : How to measure the running time of a code section in Java?
Answered by : wissam-fawaz
import java.time.Duration;
import java.time.Instant;
public class RunningTime { public static void main(String[] args) { // Create a first time stamp Instant start = Instant.now(); // Time-consuming code for(int idx = 1; idx <= 1000_000_000; idx++) { System.out.println(idx); } // Create a second time stamp Instant end = Instant.now(); // Measure time elapsing between 2 time stamps System.out.println("Running time in ms: " + Duration.between(start, end)); }
}
Source : | Last Update : Sat, 23 Apr 22