Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

How to measure the running time of a code section in Java?

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));
	}
}
 
PREVIOUS NEXT
Tagged: #How #measure #running #time #code #section
ADD COMMENT
Topic
Name
6+8 =