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】
2. 极Guest College Java Video Tutorial
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!