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. hibernate调用mysql存储过程

    在mysql中创建两个存储过程,如下: 1.根据id查找某条数据: )) begin select * from emp where empId=id; end; 2.根据id查找某个字段,并返回 ) ...

  2. 解决ODI 12C Studio 运行缓慢问题

    一.配置 ODI 12C Studio 1.1 修改ODI Studio process的-Xms和-Xmx ide.conf: modifying the initial Heap size (-X ...

  3. oracle中解决角色PLUSTRACE不存在

    在sqlplus中用autotrace查看执计划时出现如下错误提示: SYS@CDB$ROOT> conn scott/tiger@pdborcl Connected.会话已更改. SCOTT@ ...

  4. soapui not supported the auto complete

    http://forum.soapui.org/viewtopic.php?t=19850 syntax highlighting or content assist inside soapUI? t ...

  5. MYSQL数据库注释

    //修改注释 alter table user comment = '我要修改注释'; //新建表设定表注释及解释说明. create table AuthUser( ID ) primary key ...

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

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

  7. nginx 代理静态资源报 403

    用tomcat跑了一个上传服务,文件上传到指定nginx的html目录,用nginx来代理静态资源,结果上传能够成功,访问却报403. 解决办法,将html的拥有者改成nobody: chown -R ...

  8. Oracle常用方法

    oracle常用函数整理 时间转换 to_char to_date select to_char( sysdate, 'yyyy-mm') FROM dual; -- 2014-05 select t ...

  9. 向大家介绍15个漂亮的Ubuntu GDM主题

    没事向大家介绍几个Ubuntu GDM主题,希望大家喜欢,这些Ubuntu GDM主题是我找了很久的…… "GNOME Display Manager允许用户轻松的设定登录界面主题.网上有大 ...

  10. Linux init进程学习 转

    http://oss.org.cn/kernel-book/ch13/13.6.1.htm init进程的建立 Linux将要建立的第一个进程是init进程,建立该进程是以调用kernel_threa ...