1
我的代码看起来是这样的:如何使用计时器计算累加器计时器的算法?
std::chrono::milliseconds total;
for (int i=0; i<max; i++) {
auto start = std::chrono::high_resolution_clock::now();
// some code I want to benchmark
total += std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - start);
// some code I don't want to account for in my benchmark
}
std::cout << total.count() << std::endl;
当我运行这段代码我有时会收到2,3和4连作为一个结果。其他时候,我得到一些随机结果,如139984111729355或-4800608627507701400。
这是为什么?
尝试初始化您的'total'变量 –
这种基准测试有点“粗糙和准备好”。如果你想要更准确的东西,Google就会发布一个令人惊叹的[Benchmarker Library](https://github.com/google/benchmark),它非常易于使用。 Chandler Carruth在YouTube上做了一个很棒的演讲。 – Treebeard