我如何基准测试C ++代码的性能? [英] How can I benchmark the performance of C++ code?
问题描述
我开始认真研究算法和数据结构,并对学习如何比较实现A& DT的不同方式的性能感兴趣。
I am starting to study algorithms and data structures seriously, and interested in learning how to compare the performance of the different ways I can implement A&DTs.
简单的测试,我可以获得运行某事物之前/之后的时间,运行该事物10 ^ 5次,然后平均运行时间。我可以按大小对输入进行参数化,或者对随机输入进行采样,并获得运行时间与输入大小的列表。我可以将其输出为csv文件,然后将其输入到熊猫中。
For simple tests, I can get the time before/after something runs, run that thing 10^5 times, and average the running times. I can parametrize input by size, or sample random input, and get a list of running times vs. input size. I can output that as a csv file, and feed it into pandas.
我不确定是否有警告。我也不确定如何测量空间复杂度。
I am not sure there are no caveats. I am also not sure what to do about measuring space complexity.
我正在学习用C ++编程。
I am learning to program in C++. Are there humane tools to achieve what I am trying to do?
推荐答案
基准测试代码并不容易。我发现最有用的是 Google基准库。即使您不打算使用它,也可能是很好地阅读了一些例子。参数化测试,将结果输出到文件甚至返回给您算法的大O表示复杂度(仅举几个例子)的可能性很大。如果您熟悉Google测试框架,建议您使用它。
Benchmarking code is not easy. What I found most useful was Google benchmark library. Even if you are not planning to use it, it might be good to read some of examples. It has a lot of possibilities to parametrize test, output results to file and even returning you Big O notation complexity of your algorithm (to name just few of them). If you are any familiar with Google test framework I would recommend you to use it. It also keeps compiler optimization possible to manage so you can be sure that your code wasn't optimized away.
在 CppCon 2015:Chandler Carruth正在调整C ++:基准,CPU和编译器!我的天哪! 。对于可能犯的错误,您有很多见解(它也使用Google基准)
There is also great talk about benchmarking code on CppCon 2015: Chandler Carruth "Tuning C++: Benchmarks, and CPUs, and Compilers! Oh My!". There are many insights in possible mistake that you can make (it also uses google benchmark)
这篇关于我如何基准测试C ++代码的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!