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)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...
随机推荐
- 将Spring-boot应用部署到Docker容器
1:Docker中设置阿里云加速 使用阿里云的加速器,因为在使用docker的时候,会需要从docker的网站下载镜像文件,下载速度可能会很慢.获得阿里云加速,需要登录阿里云开发者平台,然后点击右侧的 ...
- MySQL快速建立测试表
1:只要已经存在表结构的 第一种方式: CREATE TABLE T1 SELECT * FROM mysql.user ; 第二种方式: CREATE TABLE T2 LIKE mysql.use ...
- SQL Server复制故障(1)
初始化时报错 错误消息: The process could not read file '\\176WINDOWSSQL\ReplData\unc\176WINDOWSSQL_DB2_TEST3\2 ...
- Centos 6下安装Oracle 11gR2
一.安装环境 CentOS release 6.7 (Final) Oracle Database 11g Release 2 二.安装前准备 #修改主机名 修改/etc/sysconfig/netw ...
- 【Linux】cat充当vi使用(特殊用法)
1.在终端输入cat >b.txt Linux:/qinys/data # cat >b.txt 2.回车后按下ctrl+d组合键即可完成编辑
- PL/SQL Developer 中文乱码问题的解决
分三个步骤解决: 1.检查server编码: 运行SQL语法: select * from v$nls_parameters; 2.设置本地client编码: 进入 ...
- startActivityForResult的使用和用法
startActivityForResult的使用和用法 startActivityForResult 和 onActivityResult在activity间传递数据 AndroidManifest ...
- Python 中的__new__和__init__的区别
[同] 二者均是Python面向对象语言中的函数,__new__比较少用,__init__则用的比较多. [异] __new__是在实例创建之前被调用的,因为它的任务就是创建实例然后返回该实例对象,是 ...
- Librec的AoBPR算法实现
Librec的AoBPR算法实现:(基于1.3版本) 要用AoBPR,但是没有找到相应的配置文件,应该怎么办呢? ——因为用的是1.3版本,所以没有,2.0版本有的.[跟BPR参数一样,就 ...
- IOCP笔记
IOCP是win32下的异步IO,利用线程池来异步处理IO请求. 这里要分析一下异步调用,跟同步调用不同,异步调用 调用了就马上返回,但是还留下个话:有事情了马上通知我,我会处理滴.恩恩,这很符合我的 ...