前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java精确测量代码运行时间 代码执行时间 纳秒 nanoTime

Java精确测量代码运行时间 代码执行时间 纳秒 nanoTime

作者头像
用户1258909
发布2018-07-03 11:40:20
1.1K0
发布2018-07-03 11:40:20
举报
文章被收录于专栏:拂晓风起拂晓风起拂晓风起

Java精确测量代码运行时间:

        long startTime = System.nanoTime();  //開始時間         for(int i = 0;i<10000;i++){             ;         }         long consumingTime = System.nanoTime() - startTime; //消耗時間         System.out.println(consumingTime);         System.out.println(consumingTime/1000+"微秒");

JDK的解释:

public static long nanoTime()

Returns the current value of the most precise available system timer, in nanoseconds.

This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so values may be negative). This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change. Differences in successive calls that span greater than approximately 292 years (263 nanoseconds) will not accurately compute elapsed time due to numerical overflow.

For example, to measure how long some code takes to execute:

   long startTime = System.nanoTime();   // ... the code being measured ...   long estimatedTime = System.nanoTime() - startTime; 

返回:

The current value of the system timer, in nanoseconds.

自从:

1.5

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2010-06-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档