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/
How to calculate elapsed / execute time in Java的更多相关文章
- linux出现bash: ./java: cannot execute binary file 问题的解决办法
问题现象描述: 到orcal官网上下载了两个jdk: (1)jdk-7u9-linux-i586.tar.gz ------------>32位 (2)jdk-7u9-linux-x64.tar ...
- Java开发中的23种设计模式详解
[放弃了原文访问者模式的Demo,自己写了一个新使用场景的Demo,加上了自己的理解] [源码地址:https://github.com/leon66666/DesignPattern] 一.设计模式 ...
- Java开发中的23种设计模式详解(转)
设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...
- java中的23中设计模式(转)
设计模式(Design Patterns) --可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...
- java开发中的23中设计模式详解--大话设计模式
设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...
- Java之设计模式详解 (转)
转载:http://blog.csdn.net/zhangerqing/article/details/8194653 设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模 ...
- java设计模式。。。转载
maowang I am a slow walker,but I never walk backwards! 博客园 首页 新随笔 联系 订阅 管理 随笔 - 125 文章 - 0 评论 - 12 ...
- java中的23中设计模式(转载的,有时间一定要熟读)
设计模式(Design Patterns) --可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...
- Java开发中的23种设计模式(转)
设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...
随机推荐
- 【树莓派】Squid代理以及白名单配置
Squid安装: sudo apt-get install squid3 -y 首先,建议备份一下这个配置文件,以免配错之后,无法恢复,又得重新安装: sudo cp /etc/squid3/squi ...
- 微软BI 之SSAS 系列 - 实现Cube 以及角色扮演维度,度量值格式化和计算成员的创建
在熟悉完下面这三种维度的创建方式之后,就可以开始创建我们的第一个 Cube 了. SSAS 系列 - 自定义的日期维度设计 SSAS 系列 - 基于雪花模型的维度设计 SSAS系列 - 关于父子维度的 ...
- 在Windows中监视IO性能
附:在Windows中监视IO性能 本来准备写一篇windows中监视IO性能的,后来发现好像可写的内容不多,windows在细节这方面做的不是那么的好,不过那些基本信息还是有的. 在Windows中 ...
- C++ 第九课 标准c数学函数
abs() 求绝对值 acos() 求反余弦 asin() 求反正弦 atan() 求反正切 atan2() 求反正切,按符号判定象限 ceil() 求不小于某值的最小整数 (求上界) cos() 求 ...
- stopPropagation 和stopImmediatePropagation区别
1.示例代码 <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UT ...
- android N : UnsatisfiedLinkError 只能访问设置为公用库的so库
在android N上使用 .so作为apk的第三方库的时候,会发生java.lang.UnsatisfiedLinkError: 09-27 12:17:01.280 D/ListenSoundMo ...
- JVM中java实例对象在内存中的布局
普通的Java对象实例 和 Java数组实例.Java数组实例的对象头多了一个数组的长度.Java虚拟机可以通过普通java对象的元数据来确定java对象的大小,但是从数组的元数据中却无法确定数组的 ...
- 转 configure: error: Cannot find ldap libraries in /usr/lib 解决办法
今天在centos 6.2 64位版本上安装LNMP,config php的时候出现下面错误而退出 configure: error: Cannot find ldap libraries in /u ...
- Libevent官方代码样例学习(一)
在Ubuntu16.04中, libevent的默认安装位置为 #在 trusty 发行版中 amd64 硬件架构下的 libevent- 软件包文件清单 /usr/lib/x86_64-linux- ...
- JS正则替换掉小括号及内容
正則表達式:\ ( [ ^ \ ) ] * \ ) JS代码: var str="hello(world)"; var nstr = str.replace(/\([^\)]*\) ...