Java 如何从减去 2 nanoTime 对象中获得有意义的结果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3654848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a meaningful result from subtracting 2 nanoTime objects?
提问by Blankman
I created a filter that monitors the length of a request.
我创建了一个过滤器来监控请求的长度。
long start = System.nanoTime();
...
long end = System.nanoTime();
How can I get the number of milliseconds from this now?
我现在如何从中获得毫秒数?
采纳答案by Chris Lercher
(end - start) / 1000000
1 microsecond = 1000 nanoseconds
1 微秒 = 1000 纳秒
1 millisecond = 1000 microseconds
1 毫秒 = 1000 微秒
Note, that the result will be rounded down, but you usually don't get true nanosecond accuracy anyway (accuracy depends on the OS). From the Javadoc on nanoTime()
:
请注意,结果将被四舍五入,但您通常不会获得真正的纳秒精度(精度取决于操作系统)。从 Javadoc 开始nanoTime()
:
This method provides nanosecond precision, but not necessarily nanosecond accuracy.
此方法提供纳秒精度,但不一定提供纳秒精度。
回答by Nikita Rybak
Just subtract them and divide result by 10^6.
只需减去它们并将结果除以 10^6。
1 nanosecond is 10^-9 seconds and, correspondingly, 10^-6 milliseconds.
http://en.wikipedia.org/wiki/Nano-
1 纳秒是 10^-9 秒,相应地,10^-6 毫秒。
http://en.wikipedia.org/wiki/Nano-
回答by harto
You could just use System.currentTimeMillis()
.
你可以只使用System.currentTimeMillis()
.
Caveat:
警告:
Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.
请注意,虽然返回值的时间单位是毫秒,但该值的粒度取决于底层操作系统,可能会更大。例如,许多操作系统以几十毫秒为单位测量时间。
回答by emory
To get a meaningful result:
要获得有意义的结果:
void procedure ( ... )
{
...
}
double measureProcedure ( double epsilon , ... )
{
double mean ;
double stderr = 2 * epsilon ;
while ( stderr > epsilon )
{
long start = System.nanoTime();
procedure ( ... ) ;
long end = System.nanoTime();
// recalculate mean , stderr
}
return ( mean / 1000000 ) ;
}
回答by BoBoCoding
Also note that you can use the TimeUnit class to help with conversion. With older versions of Java, the following code might be an example to transform a processing time into some other time format:
另请注意,您可以使用 TimeUnit 类来帮助进行转换。对于旧版本的 Java,以下代码可能是将处理时间转换为其他时间格式的示例:
long startTime = System.nanoTime();
//Processing in-between.
long endTime = System.nanoTime();
long duration = endTime - startTime;
duration = TimeUnit.SECONDS.convert(duration, TimeUnit.NANOSECONDS);
Note that newer versions of Java have shortcuts in the TimeUnit class.
请注意,较新版本的 Java 在 TimeUnit 类中有快捷方式。
The above sample will turn nanoseconds long into seconds. Also note that this truncates it so you do lose some precision. Therefore, if you switch to minutes then you will lose the precision of seconds. If you want to get a result of "12 minutes and 32 seconds" then you would have to do further processing with this solution.
上面的示例会将纳秒长转换为秒。另请注意,这会截断它,因此您确实会失去一些精度。因此,如果您切换到分钟,那么您将失去秒的精度。如果您想获得“12 分 32 秒”的结果,则必须使用此解决方案进行进一步处理。
回答by YujiSoftware
TimeUnit.NANOSECONDS.toMillis(end - start);
That's all!
就这样!