Java高精度秒表
我遇到了这个问题,但我不知道如何解决它。
我必须测量执行某些功能所需的时间。好吧,以毫秒为单位测量时间的秒表功能还不够好,所以我使用了以纳秒为单位的测量。
问题是,函数结束得如此之快,甚至纳秒级的秒表也无法测量它。
我知道秒表有效,因为我尝试将例如 Thread.sleep(1) 放入我的函数中(Thread.sleep 以毫秒为单位),并且我得到了时间,但没有 Thread .sleep,我的时间总是0。有什么想法吗?
这是我的代码:
long startTimeLocalNS=0;
long stopTimeLocalNS = 0;
startTimeLocalNS = System.nanoTime();
if (something)
{
/*my Code;*/
}
stopTimeLocalNS = System.nanoTime();
disconnectTime = (stopTimeLocalNS - startTimeLocalNS);
I have this problem, and I can't get any idea how to solve it.
I have to measure the time needed to perform certain function. Okay, stopwatch function that measure time in milliseconds wasn't good enough, so I used measurement in nano seconds.
The problem is, function ends so fast, even stopwatch in nano seconds can't measure it.
I know that stopwatch works, because I tried to put for example Thread.sleep(1)
in my function (Thread.sleep is in milliseconds), and I get my time, but without Thread.sleep
, my time is always 0. Any ideas?
Here is my code:
long startTimeLocalNS=0;
long stopTimeLocalNS = 0;
startTimeLocalNS = System.nanoTime();
if (something)
{
/*my Code;*/
}
stopTimeLocalNS = System.nanoTime();
disconnectTime = (stopTimeLocalNS - startTimeLocalNS);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在循环中运行函数几千(或百万)次,测量总时间并将其除以迭代次数怎么样?
但请注意编写微基准的许多陷阱。
How about running the function a few thousand (or million) times in a loop, measuring the total time and dividing it by the number of iterations?
But beware the many pitfalls in writing microbenchmarks.
你可以这样做:
You could do something like this:
您可以循环执行待测量代码 1000 次,然后将 nanoTime 差值除以 1000。
You can loop through the to-be-measured code 1000 times, then divide the nanoTime-difference by 1000.
您正在分析一些代码吗?如果是这样,市场上提供的配置文件会做得更好。
如果您只需要测量非常短的时间段,您有多种选择:
System.currentTimeMillis()
- 在我的计算机上,它具有 60Hz 分辨率,提供大约 16ms 的粒度System.nanoTime ()
- 应该更准确,但我听说在多核CPU时代它不是最快的解决方案Are you profiling some code? If so, profiles available on the market will do a much better job.
If you just need to measure very short period of time, You have several options:
System.currentTimeMillis()
- on my computer it has a 60Hz resolution, which gives about 16ms granularitySystem.nanoTime()
- suppose to be more accurate, but I heard that in the age of multi-core CPUs it's not the fastest solution如果您想对函数进行基准测试,最简单的方法是在一次测量中多次运行它。然后将时间除以通话次数。
If you want to benchmark your function easiest way is to run it several times inside one measurement. Then divide time by amount of calls.
根据 JVM 是否预热,您将得到截然不同的答案。对于非常短的序列,您将多次调用。你应该计算重复它们需要多长时间。
You will get a very different answer depending on whether the JVM is warmed up or not. For very short sequences which you will be calling many times. You should time how long it takes to repeat them.
左边的答案:正如 Peter Lawrey 所说,在比较两次对
nanoTime()
的调用时,大多数系统应该返回大于零的数字。但是,您绝对确定您正在比较正确的值吗?您是否不小心这样做了:那么,我的问题是,您的代码中是否存在未在上面的示例代码中重现的错误?
第二个问题,你确定
something==true
吗?An answer out of left field: as Peter Lawrey states, most systems should be returning a number greater than zero for a result when comparing two calls to
nanoTime()
. However, are you absolutely certain that you are comparing the right values? Could you accidentally be doing this:So, my question is, is there a bug in your code that you've not reproduced in the sample code above?
Second question, are you certain that
something==true
?