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/

How to calculate elapsed / execute time in Java的更多相关文章

  1. linux出现bash: ./java: cannot execute binary file 问题的解决办法

    问题现象描述: 到orcal官网上下载了两个jdk: (1)jdk-7u9-linux-i586.tar.gz ------------>32位 (2)jdk-7u9-linux-x64.tar ...

  2. Java开发中的23种设计模式详解

    [放弃了原文访问者模式的Demo,自己写了一个新使用场景的Demo,加上了自己的理解] [源码地址:https://github.com/leon66666/DesignPattern] 一.设计模式 ...

  3. Java开发中的23种设计模式详解(转)

    设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...

  4. java中的23中设计模式(转)

    设计模式(Design Patterns) --可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...

  5. java开发中的23中设计模式详解--大话设计模式

    设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...

  6. Java之设计模式详解 (转)

    转载:http://blog.csdn.net/zhangerqing/article/details/8194653 设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模 ...

  7. java设计模式。。。转载

    maowang I am a slow walker,but I never walk backwards! 博客园 首页 新随笔 联系 订阅 管理 随笔 - 125  文章 - 0  评论 - 12 ...

  8. java中的23中设计模式(转载的,有时间一定要熟读)

    设计模式(Design Patterns) --可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...

  9. Java开发中的23种设计模式(转)

    设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...

随机推荐

  1. 微信小程序 - 怎样合理设计小程序

    假如我们无意中,把腾讯地图或者高德地图的管理Key删了! 关于定位的一切相关模块就都会报废! 接着呢?客户会找你,对你公司信任感下降,一系列问题接踵而来 最好的办法就是先预留key后台管理 “随时可以 ...

  2. NUMA and vNUMA

    NUMA and vNUMA posted by szamosattila on march 04, 2012Tutorial, Virtualization With the spread of S ...

  3. Windows下的Qt Creator的安装

    采用Qt和Qt creator分别下载和安装的方式:(需要手动设置关联Qt和Qt Creator)   一.软件下载 从http://qt-project.org/downloads分别下载Qt和Qt ...

  4. 简单的tcp聊天

    package com.pers.tcptest; import java.io.IOException; import java.io.InputStream; import java.io.Out ...

  5. MongoDB高可用架构:Replica Sets+Sharding

    MongoDB的sharding解决了海量存储和动态扩容的问题.但是遇到单点故障就显得无能为力了.MongoDB的副本集可以很好的解决单点故障的问题.所以就有了Sharding+Replica Set ...

  6. unique_ptr与std::move的使用

    形参为unique_ptr u2,而后实参为std::move(unique_ptr u1),这样会将原本u1的内存传递给u2,避免了传递拷贝.例如: void fun(std::unique_ptr ...

  7. ScrollView嵌套EditText联带滑动的解决的方法

    本篇文章的相关内容需结合上文:从ScrollView嵌套EditText的滑动事件冲突分析触摸事件的分发机制以及TextView的简要实现和冲突的解决的方法 在说完了怎样解决ScrollView嵌套E ...

  8. 转 解决:error: Cannot find libmysqlclient_r under /usr/local/mysql.

    配置php的时候出现以下问题解决方案 checking for MySQL support... yeschecking for specified location of the MySQL UNI ...

  9. C# 7 out variables, tuples & other new features

    C# 7 out variables, tuples & other new features C# 7 is available on new Visual Studio 2017 and ...

  10. Kibana 日志查询

    1 概述 很多系统的日志都会放在 Kibana 供查询,就是所谓的 ELK.Kibana 除了可以使用界面供的一些 tab 或者 button 去筛选日志,也可以在搜索栏中使用 Lucene 的语法简 ...