`
wyzuomumu
  • 浏览: 104938 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

测试程序中代码的运行时间

    博客分类:
  • JAVA
阅读更多

闲着没事就想测试下程序运行代码的时间,搜了两个测试代码的方法,和大家一起分享:

1.以毫秒为单位(ms)

public static void main(String[] args) {
//get start time
long startTime = System.currentTimeMillis();
//test the code
test code
//get the end time
long endTime = System.currentTimeMillis();
System.out.println("take the time is: " + (endTime - startTime) + " ns")
}

 

2.以纳秒为单位

public static void main(String[] args) {
//get start time
long startTime = System.nanoTime();
//test the code
test code
//get the end time
long endTime = System.nanoTime();
System.out.println("take the time is: " + (endTime - startTime) + " ms")
}

 

END......

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics