为什么System.nanoTime()在迭代开始时不正确?(Why is System.nanoTime() incorrect in the beginning of iterations?)

编程入门 行业动态 更新时间:2024-10-25 19:37:18
为什么System.nanoTime()在迭代开始时不正确?(Why is System.nanoTime() incorrect in the beginning of iterations?)

我注意到System.nanoTime()的模式。 每次我开始迭代时,nanoTime()都会在几圈之后变得非常不正确,直到它最终稳定下来。

如果我例如运行下面的代码:

public class TimeTest{ public static void main(String[] args) { long prev = System.nanoTime(); for(int i = 0; i < 10; i++) { for(int j = 0; j < 1000000; j++); long time = System.nanoTime(); System.out.println(time - prev); prev = time; } } }

我得到以下结果:

为了消除Sys​​tem.out.println(String)混淆结果的可能性,我还可以运行以下测试:

public class TimeTest{ public static void main(String[] args) { long[] difs = new long[10]; long prev = System.nanoTime(); for(int i = 0; i < 10; i++) { for(int j = 0; j < 1000000; j++); long time = System.nanoTime(); difs[i] = (time - prev); prev = time; } for(long l : difs) System.out.println(l); } }

这给出了以下结果:

初始延迟可能可以通过假设迭代开始(在这种情况下为for循环)在循环开始之前花费额外的时间来初始化来解释。 然而,由于第二圈也可能需要很长时间才能执行,我们可以相信它可能不是最终的循环。

所以我的问题很简单,当System.nanoTime()与迭代一起使用时,导致此初始延迟的原因是什么?

注意:我也尝试了不同类型的迭代器,但问题依然存在。

I've noticed a pattern with System.nanoTime(). Everytime I begin an iteration, the nanoTime() gets hugely incorrect for a couple of laps until it finally stabilises.

If I for instance run the following code:

public class TimeTest{ public static void main(String[] args) { long prev = System.nanoTime(); for(int i = 0; i < 10; i++) { for(int j = 0; j < 1000000; j++); long time = System.nanoTime(); System.out.println(time - prev); prev = time; } } }

I get the following result:

To eliminate the possibility of System.out.println(String) messing with the result, I can also run the following test:

public class TimeTest{ public static void main(String[] args) { long[] difs = new long[10]; long prev = System.nanoTime(); for(int i = 0; i < 10; i++) { for(int j = 0; j < 1000000; j++); long time = System.nanoTime(); difs[i] = (time - prev); prev = time; } for(long l : difs) System.out.println(l); } }

Which gives the following result:

The initial delay could possibly be explained by assuming that the beginning of an iteration (in this case the for-loop) is taking some extra time to initialize before the loop begins. However, since the second lap supposedly also takes a long time to execute, we can believe that it might not be the for-loop after all.

So my question is simply, what is causing this initial delay when using System.nanoTime() together with iterations?

Note: I've also tried different types of iterators, but the problem remains.

最满意答案

这看起来像JVM的JIT预热时间

当Java HotSpot编译器在您的代码中看到“热点”时,它就会启动它。 因此,随着时间的推移,您的代码运行速度会很快! 所以,你应该调整你的测试方法。

HotSpot编译器在后台编译,消耗CPU周期。 所以当编译器很忙时,你的程序暂时变慢。 但是在编译一些热点之后,你的程序会突然跑得更快!

That looks to me like the JIT warm up time of the JVM

The Java HotSpot compiler kicks in when it sees a ‘hot spot’ in your code. It is therefore quite common that your code will run faster over time! So, you should adapt your testing methods.

The HotSpot compiler compiles in the background, eating away CPU cycles. So when the compiler is busy, your program is temporarily slower. But after compiling some hot spots, your program will suddenly run faster!

更多推荐

本文发布于:2023-07-31 01:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340445.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不正确   迭代   nanoTime   System   beginning

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!