Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 比较算法';执行时间:为什么执行顺序很重要?_C++_Algorithm_Profiling_Performance Testing - Fatal编程技术网

C++ 比较算法';执行时间:为什么执行顺序很重要?

C++ 比较算法';执行时间:为什么执行顺序很重要?,c++,algorithm,profiling,performance-testing,C++,Algorithm,Profiling,Performance Testing,每当我尝试比较两种竞争算法的执行时间时(使用C++),我都会使用以前在这个问题中建议的std::chrono: 然而,我总是注意到,被比较的算法的执行顺序会显著影响执行时间。它甚至经常改变哪种竞争算法被认为是最快的。例如,假设我有两个算法algo1和algo2 我的意思是下面的代码: std::chrono::high_resolution_clock::time_point start0, start1; std::chrono::high_resolution_clock::time_poi

每当我尝试比较两种竞争算法的执行时间时(使用C++),我都会使用以前在这个问题中建议的
std::chrono

然而,我总是注意到,被比较的算法的执行顺序会显著影响执行时间。它甚至经常改变哪种竞争算法被认为是最快的。例如,假设我有两个算法
algo1
algo2

我的意思是下面的代码:

std::chrono::high_resolution_clock::time_point start0, start1;
std::chrono::high_resolution_clock::time_point end0, end1;

start1 = std::chrono::high_resolution_clock::now();
algo1();
end1 = std::chrono::high_resolution_clock::now();

start2 = std::chrono::high_resolution_clock::now();
algo2();
end2 = std::chrono::high_resolution_clock::now();

auto time_elapsed1 = std::chrono::duration_cast<std::chrono::nanoseconds>(end1 - start1).count();
auto time_elapsed2 = std::chrono::duration_cast<std::chrono::nanoseconds>(end2 - start2).count();
std::chrono::high_resolution_clock::time_point start0, start1;
std::chrono::high_resolution_clock::time_point end0, end1;

start2 = std::chrono::high_resolution_clock::now();
algo2();
end2 = std::chrono::high_resolution_clock::now();

start1 = std::chrono::high_resolution_clock::now();
algo1();
end1 = std::chrono::high_resolution_clock::now();

auto time_elapsed1 = std::chrono::duration_cast<std::chrono::nanoseconds>(end1 - start1).count();
auto time_elapsed2 = std::chrono::duration_cast<std::chrono::nanoseconds>(end2 - start2).count();
std::chrono::高分辨率时钟::时间点开始0,开始1;
std::chrono::高分辨率时钟::时间点end0,end1;
start1=std::chrono::高分辨率时钟::now();
algo1();
end1=std::chrono::高分辨率时钟::现在();
start2=std::chrono::高分辨率时钟::now();
algo2();
end2=std::chrono::高分辨率时钟::now();
自动时间延迟1=std::chrono::duration_cast(end1-start1).count();
自动计时2=std::chrono::duration_cast(end2-start2).count();
给出与以下代码不同的结果:

std::chrono::high_resolution_clock::time_point start0, start1;
std::chrono::high_resolution_clock::time_point end0, end1;

start1 = std::chrono::high_resolution_clock::now();
algo1();
end1 = std::chrono::high_resolution_clock::now();

start2 = std::chrono::high_resolution_clock::now();
algo2();
end2 = std::chrono::high_resolution_clock::now();

auto time_elapsed1 = std::chrono::duration_cast<std::chrono::nanoseconds>(end1 - start1).count();
auto time_elapsed2 = std::chrono::duration_cast<std::chrono::nanoseconds>(end2 - start2).count();
std::chrono::high_resolution_clock::time_point start0, start1;
std::chrono::high_resolution_clock::time_point end0, end1;

start2 = std::chrono::high_resolution_clock::now();
algo2();
end2 = std::chrono::high_resolution_clock::now();

start1 = std::chrono::high_resolution_clock::now();
algo1();
end1 = std::chrono::high_resolution_clock::now();

auto time_elapsed1 = std::chrono::duration_cast<std::chrono::nanoseconds>(end1 - start1).count();
auto time_elapsed2 = std::chrono::duration_cast<std::chrono::nanoseconds>(end2 - start2).count();
std::chrono::高分辨率时钟::时间点开始0,开始1;
std::chrono::高分辨率时钟::时间点end0,end1;
start2=std::chrono::高分辨率时钟::now();
algo2();
end2=std::chrono::高分辨率时钟::now();
start1=std::chrono::高分辨率时钟::now();
algo1();
end1=std::chrono::高分辨率时钟::现在();
自动时间延迟1=std::chrono::duration_cast(end1-start1).count();
自动计时2=std::chrono::duration_cast(end2-start2).count();
对于我想比较的几乎所有算法1和2

因此,我的问题有两个方面:1)为什么会这样,即为什么顺序很重要?2) 是否有更好的方法来比较两种算法的执行时间,即如何进行更好、更准确的比较


PS:当然,我总是在所有编译器的优化都打开的情况下进行测试。

这很可能是由于缓存

通过多次运行相同的算法,可以轻松验证缓存的效果。您可能会注意到,第一次执行所花费的时间比后续执行所花费的时间要长得多

当我不得不为我的博士论文比较两个算法时,我一行执行了10次每一个算法,丢弃了第一个结果,然后平均了剩余的9个结果,这9个结果非常一致


第一个被丢弃的结果是否重要是有争议的,但对我来说并不重要,因为我更感兴趣的是比较两个算法的相对性能(因此寻找每个算法的一致运行时间)而不是测量缓存的影响或在不同情况下每个算法的绝对性能。

您需要多次运行该算法并获得平均值。某些东西可能会加载到缓存中,从而影响后续的运行。您必须确保算法运行足够长的时间,以获得有意义的时间间隔。因此,需要注意的是数据的缓存。尝试连续两次运行每个algo(),第二次运行计时。我假设您的函数有明显的副作用。否则,它们可能会被完全优化掉。另外,正如@NathanOliver所说,将每个算法运行数千次/百万次,得到平均值/平均值。基准测试异常困难;)@JesperJuhl可能是一个好的基准测试产生硬数字的原因。对我来说,诀窍就是你的建议:不仅要多次运行算法,还要丢弃初始值。更精确-可以针对任何分析问题进行验证,这些问题的实际解是事先已知的。谢谢