简体   繁体   English

在几个方法调用周围使用System.nanoTime()所耗用的时间所提供的值比每种方法所耗用的时间总和低吗?

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

I'm using System.nanoTime() to measure the time it takes to call several methods. 我正在使用System.nanoTime()测量调用多个方法所花费的时间。 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. 但是,事实并非如此。

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+. 就我而言,总时间约为950ms,但是每次经过的时间总和为1300ms +。 Why is this? 为什么是这样?

EDIT: 编辑:

Okay, to be a bit clearer, I am not getting 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). 我将两个非常大的文本文件读入String数组(第一个文件中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. EDIT2:我的错误是,我在方法中调用方法,并对这些时间进行了总结,这些时间已经包含在内。 Without these double-times it all adds up. 没有这些双重时间,所有这些加起来。 Thanks for clearing this up! 感谢您清除此问题!

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 - GlobalStart to be greater than or equal to (End1-Start1) + (End2-Start2) + (End3-Start3) . 为什么我建议您执行以下操作:您希望GlobalEnd - GlobalStart大于或等于(End1-Start1) + (End2-Start2) + (End3-Start3) But this relation actually derives from the fact that if everything is sequential the following holds true: 但是这种关系实际上源于以下事实:如果一切都是连续的,则以下条件成立:

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. 这可能会给您一些见识。

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) 方法1:600方法2:500方法3:10总时间:1110成功建立(总时间:2秒)

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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM