zoukankan      html  css  js  c++  java
  • How to calculate elapsed / execute time in Java

    How to calculate elapsed / execute time in Java
    In Java, you can use the following ways to measure elapsed time in Java.

    1. System.nanoTime()
    This is the recommended solution to measure elapsed time in Java.

    ExecutionTime1.java
    package com.mkyong.time;

    import java.util.concurrent.TimeUnit;

    public class ExecutionTime1 {

    public static void main(String[] args) throws InterruptedException {

    //start
    long lStartTime = System.nanoTime();

    //task
    calculation();

    //end
    long lEndTime = System.nanoTime();

    //time elapsed
    long output = lEndTime - lStartTime;

    System.out.println("Elapsed time in milliseconds: " + output / 1000000);

    }

    private static void calculation() throws InterruptedException {

    //Sleep 2 seconds
    TimeUnit.SECONDS.sleep(2);

    }
    }

    Output may vary.

    2004

    2. System.currentTimeMillis()
    ExecutionTime2.java
    package com.mkyong.time;

    import java.util.concurrent.TimeUnit;

    public class ExecutionTime2 {

    public static void main(String[] args) throws InterruptedException {

    long lStartTime = System.currentTimeMillis();

    calculation();

    long lEndTime = System.currentTimeMillis();

    long output = lEndTime - lStartTime;

    System.out.println("Elapsed time in milliseconds: " + output);

    }

    private static void calculation() throws InterruptedException {

    //Sleep 2 seconds
    TimeUnit.SECONDS.sleep(2);

    }
    }

    Output may vary.

    2006

    3. Instant.now().toEpochMilli()
    In Java 8, you can try the new java.time.Instant

    ExecutionTime3.java
    package com.mkyong.time;

    import java.time.Instant;
    import java.util.concurrent.TimeUnit;

    public class ExecutionTime3 {

    public static void main(String[] args) throws InterruptedException {

    long lStartTime = Instant.now().toEpochMilli();

    calculation();

    long lEndTime = Instant.now().toEpochMilli();

    long output = lEndTime - lStartTime;

    System.out.println("Elapsed time in milliseconds: " + output);

    }

    private static void calculation() throws InterruptedException {

    //Sleep 2 seconds
    TimeUnit.SECONDS.sleep(2);

    }
    }

    Output may vary.

    2006

    4. Date().getTime()
    ExecutionTime4.java
    package com.mkyong.time;

    import java.util.Date;
    import java.util.concurrent.TimeUnit;

    public class ExecutionTime4 {

    public static void main(String[] args) throws InterruptedException {

    long lStartTime = new Date().getTime();

    calculation();

    long lEndTime = new Date().getTime();

    long output = lEndTime - lStartTime;

    System.out.println("Elapsed time in milliseconds: " + output);

    }

    private static void calculation() throws InterruptedException {

    //Sleep 2 seconds
    TimeUnit.SECONDS.sleep(2);

    }
    }

    Output may vary.

    2007

    http://www.mkyong.com/java/how-do-calculate-elapsed-execute-time-in-java/
    http://www.mkyong.com/tutorials/java-date-time-tutorials/

  • 相关阅读:
    Oracle数据库系统结构二(实例结构)
    Oracle数据库系统结构一(存储结构)
    SQL Plus的使用详解(登录和常用命令)
    Oracle的基本了解和配置
    Oracle11g的安装及删除
    C++编译预处理
    C++程序的多文件组织
    C++变量的存储类型
    C++动态存储方式与静态存储方式
    C++函数五(局部变量与全局变量和域运算符)
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/10116613.html
Copyright © 2011-2022 走看看