Home  >  Article  >  Java  >  Introducing two code examples for calculating java running time

Introducing two code examples for calculating java running time

Y2J
Y2JOriginal
2017-05-09 11:58:181424browse

The editor below will bring you an example of calculating the running time of two programs in Java. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

1. Get the current system time

long startTime = System.currentTimeMillis(); //获取开始时间

 doSomething(); //测试的代码段

 long endTime = System.currentTimeMillis(); //获取结束时间

 System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); //输出程序运行时间

2. In nanoseconds Calculate in units

// 第二种是以纳秒为单位计算的。
long startTime=System.nanoTime(); //获取开始时间

doSomeThing(); //测试的代码段

long endTime=System.nanoTime(); //获取结束时间

System.out.println("程序运行时间: "+(endTime-startTime)+"ns");

【Related recommendations】

1. Java Free Video Tutorial

2. 极Guest College Java Video Tutorial

3. JAVA Tutorial Manual

The above is the detailed content of Introducing two code examples for calculating java running time. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn