如何通过减去 2 个 nanoTime 对象获得有意义的结果?

发布于 2024-09-17 10:19:56 字数 140 浏览 7 评论 0原文

我创建了一个监视请求长度的过滤器。

long start = System.nanoTime();

...

long end = System.nanoTime();

现在我怎样才能得到毫秒数?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

世界如花海般美丽 2024-09-24 10:19:56
(end - start) / 1000000

1 微秒 = 1000 纳秒

1 毫秒 = 1000 微秒

请注意,结果将向下舍入,但您通常无法获得真正的纳秒精度(精度取决于操作系统)。来自 nanoTime() 的 Javadoc:

此方法提供纳秒精度,但不
必然是纳秒级的精度。

(end - start) / 1000000

1 microsecond = 1000 nanoseconds

1 millisecond = 1000 microseconds

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():

This method provides nanosecond precision, but not
necessarily nanosecond accuracy.

执着的年纪 2024-09-24 10:19:56

TimeUnit#toMillis(long)(自 Java 5 起)

TimeUnit.NANOSECONDS.toMillis(end - start);

Duration#toMillis() (自 Java 8 起)

Duration.ofNanos(end - start).toMillis()

Duration# Between(Temporal, Temporal)(自 Java 8 起)

Instant start = Instant.now();
...
Instant end = Instant.now();

Duration.between(start, end).toMillis()

ChronoUnit.html# Between(Temporal, Temporal)(自 Java 8 起)

Instant start = Instant.now();
...
Instant end = Instant.now();

ChronoUnit.MILLIS.between(start, end)

TimeUnit#toMillis(long) (since Java 5)

TimeUnit.NANOSECONDS.toMillis(end - start);

OR

Duration#toMillis() (since Java 8)

Duration.ofNanos(end - start).toMillis()

OR

Duration#between(Temporal, Temporal) (since Java 8)

Instant start = Instant.now();
...
Instant end = Instant.now();

Duration.between(start, end).toMillis()

OR

ChronoUnit.html#between(Temporal, Temporal) (since Java 8)

Instant start = Instant.now();
...
Instant end = Instant.now();

ChronoUnit.MILLIS.between(start, end)
鹿童谣 2024-09-24 10:19:56

另请注意,您可以使用 TimeUnit 类来帮助进行转换。对于旧版本的 Java,以下代码可能是将处理时间转换为其他时间格式的示例:

long startTime = System.nanoTime();

//Processing in-between.

long endTime = System.nanoTime();
long duration = endTime - startTime;
duration = TimeUnit.SECONDS.convert(duration, TimeUnit.NANOSECONDS);

请注意,新版本的 Java 在 TimeUnit 类中具有快捷方式。

上面的示例会将纳秒长转换为秒。另请注意,这会截断它,因此您确实会损失一些精度。因此,如果切换到分钟,那么您将失去秒的精度。如果你想得到“12分32秒”的结果,那么你就必须用这个解决方案做进一步的处理。

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:

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.

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.

南笙 2024-09-24 10:19:56

只需减去它们并将结果除以 10^6 即可。

1 纳秒是 10^-9 秒,相应地,是 10^-6 毫秒。
http://en.wikipedia.org/wiki/Nano-

Just subtract them and divide result by 10^6.

1 nanosecond is 10^-9 seconds and, correspondingly, 10^-6 milliseconds.
http://en.wikipedia.org/wiki/Nano-

爱*していゐ 2024-09-24 10:19:56

您可以使用 System.currentTimeMillis()

注意事项:

请注意,虽然返回值的时间单位是毫秒,但该值的粒度取决于底层操作系统,并且可能会更大。例如,许多操作系统以数十毫秒为单位测量时间。

You could just use 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.

只是偏爱你 2024-09-24 10:19:56

要获得有意义的结果:

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 ) ;
}

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 ) ;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文