简体   繁体   English

在发布模式下,在 Visual Studio (C++) 中测量循环内方法的运行时出错

[英]Error in measuring runtime of a method inside a loop in Visual Studio (C++) in release mode

I have a method named function (see below) which has two inputs: an image and an integer which is an instruction to the function to do some operations on my image.我有一个名为 function(见下文)的方法,它有两个输入:一个图像和一个整数,它是函数对我的图像执行一些操作的指令。 Therefore, each number means a different operation.因此,每个数字意味着不同的操作。

for(int i=0;i<8;i++){

     auto start = std::chrono::high_resolution_clock::now();
     function(image, i);
     auto elapsed = std::chrono::high_resolution_clock::now() - start;
     long long microseconds =std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
     cout << "i=" << i << ", time=" << microseconds << " micro seconds" << "  or "<<microseconds/(long double)1000000<<" seconds"<<"\n";

    }

Here is the output of the program:这是程序的输出:

i=0, time=357586 micro seconds  or 0.357586 seconds
i=1, time=15624 micro seconds  or 0.015624 seconds
i=2, time=15624 micro seconds  or 0.015624 seconds
i=3, time=15625 micro seconds  or 0.015625 seconds
i=4, time=15626 micro seconds  or 0.015626 seconds
i=5, time=15624 micro seconds  or 0.015624 seconds
i=6, time=15624 micro seconds  or 0.015624 seconds
i=7, time=15625 micro seconds  or 0.015625 seconds

However, if I change the order of instruction 1 and 0, I get the following:但是,如果我更改指令 1 和 0 的顺序,则会得到以下信息:

i=1, time=348474 micro seconds  or 0.348474 seconds
i=0, time=15625 micro seconds  or 0.015625 seconds
i=2, time=15612 micro seconds  or 0.015612 seconds
i=3, time=15625 micro seconds  or 0.015625 seconds
i=4, time=15625 micro seconds  or 0.015625 seconds
i=5, time=15625 micro seconds  or 0.015625 seconds
i=6, time=15625 micro seconds  or 0.015625 seconds
i=7, time=15625 micro seconds  or 0.015625 seconds

In other words, first call of the function always takes more time irrespective of the type of operation.换句话说,无论操作类型如何,函数的第一次调用总是需要更多时间。 How can I fix this problem?我该如何解决这个问题? My goal is to compare runtime of different operations and then discard those operations that are time-consuming.我的目标是比较不同操作的运行时间,然后丢弃那些耗时的操作。

Try declaring all of the variables globally instead of locally in the loop, which may be what is taking time.尝试在循环中全局而不是局部声明所有变量,这可能需要时间。 I imagine after it sets up the variables into registers, it re-uses them, so that's why you see the initial time spike.我想在它将变量设置到寄存器中之后,它会重新使用它们,这就是为什么您会看到初始时间峰值。 However it's also possible that the optimized time is because the function pointer is already set up in registers as well and it's just passing a new parameter into it each time.然而,优化时间也可能是因为函数指针也已经在寄存器中设置了,并且每次都只是将一个新参数传递给它。 Heck for all I know it's because your CPU uses predictive pipelining, and halfway processed the function in the future iterations.据我所知,这是因为您的 CPU 使用预测流水线,并且在未来的迭代中对函数进行了中途处理。 It's difficult to say, but if you want to get a true running time, is it possible to just call your function with dummy parameters for the first iteration and discard the results?很难说,但是如果您想获得真正的运行时间,是否可以仅在第一次迭代时使用虚拟参数调用您的函数并丢弃结果?

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

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