java 围绕多个方法调用使用 System.nanoTime() 的经过时间提供的值低于每个方法中经过的时间总和?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14818765/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:29:00  来源:igfitidea点击:

Elapsed time using System.nanoTime() around several method calls delivers a lower value than the sum of of the elapsed times in each method?

java

提问by someone

I'm using System.nanoTime() to measure the time it takes to call several methods. In each of those methods I do the same to measure how long each method takes. In the end the sum of the elapsed times should be smaller than the total elapsed time, or so I thought. However, it isn't.

我正在使用 System.nanoTime() 来测量调用多个方法所需的时间。在每一种方法中,我都用同样的方法来测量每种方法需要多长时间。最后经过时间的总和应该小于总经过时间,或者我认为。然而,事实并非如此。

Example:

例子:

public static void main(String[] args){
  long startTime = System.nanoTime();
  method1();
  method2();
  method3();
  System.out.println( "Total Time: " + (System.nanoTime() - startTime) / 1000000);
}
private void method1(){
  long startTime = System.nanoTime();
  //Stuff
  System.out.println( "Method 1: " + (System.nanoTime() - startTime) / 1000000);
}
// same for all other methods

In my case I get something around 950ms for the total time, but the sum of each elapsed time is 1300ms+. Why is this?

在我的情况下,我得到的总时间约为 950 毫秒,但每个经过时间的总和为 1300 毫秒+。为什么是这样?

EDIT:

编辑:

Okay, to be a bit clearer, I am notgetting this behaviour when writing to arrays many times (which I just did as a test). When I do this, I get pretty much exact results (+-1ms).

好的,更清楚一点,在多次写入数组时我没有得到这种行为(我只是作为测试做的)。当我这样做时,我得到了非常精确的结果(+-1ms)。

What I am actually doing is this:

我实际上在做的是这样的:

I read two pretty huge text files into String arrays (1000 * ~2000 characters in first file, 200 * ~100 characters in the second).

我将两个非常大的文本文件读入字符串数组(第一个文件中为 1000 * ~2000 个字符,第二个文件中为 200 * ~100 个字符)。

I then do a whole lot of comparisons on the String array I got from reading the first file and use the results to calculate some probabilities.

然后,我对读取第一个文件得到的 String 数组进行了大量比较,并使用结果来计算一些概率。

EDIT2: Error on my part, I was calling methods within methods and summed up those times as well, which were already included. Without these double-times it all adds up. Thanks for clearing this up!

EDIT2:我的错误,我在方法中调用方法并总结了那些时间,这些时间已经包括在内。如果没有这些双倍,一切都会加起来。感谢您解决这个问题!

回答by Cyrille Ka

To further investigate this thing, maybe you can print out the start and end time of each method and of the global process. Here you are just printing the time taken by each one and the total time, but you can output something like this:

为了进一步研究这个东西,也许你可以打印出每个方法和全局进程的开始和结束时间。在这里,您只是打印每个人花费的时间和总时间,但您可以输出如下内容:

Global start   : (result of System.nanoTime() here)
Method 1 Start : ...
Method 1 End   : ....
Method 2 Start : ....
Method 2 End   : ....
Method 3 Start : ....
Method 3 End   : ....
Global end     : ....

Why I suggest you to do this is the following: you expected GlobalEnd - GlobalStartto be greater than or equal to (End1-Start1) + (End2-Start2) + (End3-Start3). But this relation actually derives from the fact that if everything is sequential the following holds true:

我建议您这样做的原因如下:您希望GlobalEnd - GlobalStart大于或等于(End1-Start1) + (End2-Start2) + (End3-Start3). 但这种关系实际上源于这样一个事实:如果一切都是顺序的,则以下内容成立:

GlobalStart <= Start1 <= End1 <= Start2 <= End2 <= Start3 <= End3 <= GlobalEnd

Isn't it?

不是吗?

Then what would be interesting for you is to know what is not true in this list of inequations. This could possibly give you some insight.

那么你会感兴趣的是知道在这个不等式列表中什么是不正确的。这可能会给你一些见解。

回答by Doug M

I see nothing wrong with your code. In my testing I got the correct elapsed time from your code.

我看不出你的代码有什么问题。在我的测试中,我从您的代码中获得了正确的经过时间。

Here is my output:

这是我的输出:

Method 1: 600 Method 2: 500 Method 3: 10 Total Time: 1110 BUILD SUCCESSFUL (total time: 2 seconds)

方法一:600 方法二:500 方法三:10 总时间:1110 BUILD SUCCESSFUL(总时间:2 秒)

回答by Doug M

Try changing your doe to something like this:

尝试将您的母牛更改为以下内容:

public static void main(String[] args)
{
    long elaspeTime = 0;
    elaspeTime += method1();
    elaspeTime += method2();
    elaspeTime += method3();
    System.out.println("Total Time: " + elaspeTime / 1000000);
}

private static long method1()
{
    long startTime = System.nanoTime();
    //Do some work here...
    System.out.println("Method 1: " + (System.nanoTime() - startTime) / 1000000);
    return System.nanoTime() - startTime;

}