x Java Java 8 JUnit JSON
  • XML
  • JDBC Spring Boot Microservices React Contact Us

    How to measure elapsed time or check time taken by method in Java ?

    1) Measure elapsed time using System currentTimeMillis()

    You can measure elapsed time or check time taken by method in Java by using System currentTimeMillis().This method returns the current time in milliseconds which is actually the difference between current time and 1st of January, 1970 UTC.

    // measure elapsed time or check time taken by method
    public class MeasureElapsedTime {
    		
        public void print() {
            System.out.println("Hello");
        }
    		
        public static void main(String[] args) {
            long startTime = System.currentTimeMillis();
            (new MeasureTime()).print();
            long endTime = System.currentTimeMillis();
            System.out.println("time elapsed in print method " +
               (endTime - startTime) + " milliseconds");	
        }
    }	

    Reference https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#currentTimeMillis

    2) Measure elapsed time using System nanoTime()

    You can also measure elapsed time or check time taken by method in Java using System nanoTime() which gives more precise time in nanoseconds.

    // measure elapsed time or check time taken by method
    public class MeasureTime {
    		
        public void print() {
            System.out.println("Hello");
        }
    		
        public static void main(String[] args) {
            long startTime = System.nanoTime();
            (new MeasureTime()).print();
            long endTime = System.nanoTime();
            System.out.println("time elapsed in print method " +
                (endTime - startTime) + " nanoseconds");	
        }
    }	

    2) Measure elapsed time using Java 8 Duration & Instant classes

    You can also measure elapsed time or check time taken by method in Java using Duration & Instant classes. Instant now() will return an instance of Instant class. You need to pass two such instances, one when the event starts and once when the event stops to Duration between() method, which will return Duration object. Duration toMillis() method will return the time in milliseconds.

    import java.time.Duration;
    import java.time.Instant;
    
    // measure elapsed time or check time taken by method
    public class MeasureTime {
    		
        public void print() {
            System.out.println("Hello");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }		
        }
    		
        public static void main(String[] args) {
            Instant start = Instant.now();
            (new MeasureTime()).print();
            Instant end = Instant.now();
            long timeElapsed = Duration.between(start, end).toMillis();
            System.out.println("time elapsed in print method " + timeElapsed + " milliseconds");	
        }
    }	

    4) Measure elapsed time using Spring Stopwatch \

    You can also measure elapsed time or check time taken by method in Java using Spring Stopwatch getTotalTimeSeconds() method which calculates time in seconds. You need to call Stopwatch start() at the start of event you want to check and stop() at the time event ends. At the end you need to call Stopwatch getTotalTimeSeconds() to get the time taken by the event.

    You need to download spring core library from here download spring-core-4.3.4.jar
    import org.springframework.util.StopWatch;
    
    // measure elapsed time or check time taken by method
    public class MeasureTime {
    		
        public void print() {
            System.out.println("Hello");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }		
        }
    		
        public static void main(String[] args) {
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            (new MeasureTime()).print();
            stopWatch.stop();
            System.out.println("time elapsed in print method " +
            stopWatch.getTotalTimeSeconds() + " seconds");
        }
    }	

    5) Measure elapsed time using Apache Commons Lang Stopwatch

    You can measure elapsed time or check time taken by method in Apache Commons Lang Stopwatch getTime() method which calculates time in milliseconds. You need to call Stopwatch start() at the start of event you want to check and stop() at the time event ends. At the end you need to call Stopwatch getTime() to get the time taken by the event.

    You need to download Apache Commons Lang library from here download commons-lang3-3.7.jar
    import org.apache.commons.lang3.time.StopWatch;
    
    // measure elapsed time or check time taken by method
    public class MeasureTime {
    		
        public void print() {
            System.out.println("Hello");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }		
        }
    		
        public static void main(String[] args) {
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            (new MeasureTime()).print();
            stopWatch.stop();
            System.out.println("time elapsed in print method " +
                stopWatch.getTime() + " milliseconds");
        }
    }	
    Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *











    Share This