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)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...
随机推荐
- 微信小程序 - 浮层引导(示例)
更新日期: 2019/3/15:首次发布,具体请下载:demo.
- Spring 在+publicId+和+systemId+之间需要有空格
今天配置Spring,遇到一个很奇葩的问题: Spring.xml的配置文件内容: <?xml version="1.0" encoding="UTF-8" ...
- 算法笔记_202:第三届蓝桥杯软件类决赛真题(Java高职)
目录 1 填算式 2 提取子串 3 机器人行走 4 地址格式转换 5 排日程 前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 填算式 [结果填空] (满分11分) 看这个算式: ☆☆☆ + ☆☆ ...
- JavaScript公共函数
[在此处输入文章标题] // JScript 文件 /* ================================================================== JS 公 ...
- mybatis批量删除(逻辑删除)
在Mapper.xml文件中 <!-- 批量删除 --> <delete id="deleteAd" parameterType="java.util. ...
- Java多线程之ReadWriteLock读写锁简介与使用教程
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6558073.html 普通的锁在对某一内容加锁后,其他线程是不能访问的.但是我们要考虑这种情况:如果当前加锁 ...
- secureCRT,永久设置,保护眼睛,配色方案
配色后效果如下: 下面开始配色 1.选项(Options)==>会话选项(Sessions options)==>终端(Terminal)==>仿真(Emulation) 按图中标注 ...
- python之函数用法startswith()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法startswith() #http://www.runoob.com/python/ ...
- Spring Cloud Edgware SR3 让Zuul支持形如 /xxx和/xxx/yyy 格式的路径配置
在包路径:org.springframework.cloud.netflix.zuul.filters 下,新建类SimpleRouteLocator,取代jar包中的类.内容如下: /* * Cop ...
- 三种常用的MySQL建表语句
MySQL建表语句是最基础的SQL语句之一,下面就为您介绍最常用的三种MySQL建表语句,如果您对MySQL建表语句方面感兴趣的话,不妨一看. 1.最简单的: CREATE TABLE t1( ...