使用< ctime>进行基准测试和指令重新排序 [英] Benchmarking using <ctime> and instruction reordering

查看:67
本文介绍了使用< ctime>进行基准测试和指令重新排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我一直在使用传统的方法来对并发方法进行基准测试,该方法是测量多次运行的经过时间:

I've been using, up until now, the traditional way to benchmark concurrent methods, which is to measure the elapsed duration for a number of runs:

template <typename Functor>
double benchmark(Functor const& f, size_t nbRuns)
{
  if (nbRuns == 0) { return 0.0; }

  f(); // Initialize before measuring, I am not interesting in setup cost

  time_t begin = time(0);
  for (size_t i = 0; i != nbRuns; ++i) { f(); }
  time_t end = time(0);

  return difftime(end, begin);
}

在我问到以下问题之前,这似乎都很好,很花哨:优化掉 while(1);在C ++ 0x中循环

which seemed all fine and dandy until I came upon this question: Optimizing away a "while(1);" loop in C++0x.

让我感到不同寻常的是,允许编译器在循环之前执行输出...突然想知道:

What strikes me as unusual is that the compiler is allowed to execute the output BEFORE the loop... and I am suddenly wondering:


是什么阻止了编译器执行 time_t end = time(0); 在这里循环之前?

因为如果这样做,那一定会弄乱我的小基准代码。

because if it did, that would somehow screw my little benchmark code.

而在这种情况下,如果在这种情况下可能会发生重新排序:

And while we are at it, if ever the reordering could occur in this situation:


一个阻止它的行为吗?

How can one prevent it ?

我想不出C ++之外的其他相关标签,如果有人认为我错过了一个,随时添加它

推荐答案

这是一个棘手的问题。


是什么导致编译器无法通过
执行time_t end = time(0);在
循环之前吗?

What prevents the compiler from executing time_t end = time(0); before the loop here ?

通常,什么也没有;实际上,即使在C ++ 03中也是如此。由于使用了as-if规则,编译器可能会发出具有相同可观察行为的任何代码。这意味着,如果省略 f()不会更改任何指定的输入/输出或volatile访问,则可能不会运行 f()

Generally, nothing; in fact, even in C++03. Because of the as-if rule, the compiler may emit any code which has the same observable behaviour. That means, if omitting f() doesn't change any specified input/output, or volatiles access, it may not run f() at all.


让我感到不同寻常的是,允许
编译器执行
在循环之前输出

What strikes me as unusual is that the compiler is allowed to execute the output BEFORE the loop

这不是真的-空循环的问题是C ++ 0x不计数只是不终止为可观察的行为。并不是说它可以对空循环和 Hello 的输出进行重新排序,而是编译器可以完全忽略空循环。

That's not really true - the issue with the empty loop is that C++0x doesn't count mere nontermination as observable behavior. It's not that it can reorder empty loop and the output of "Hello", it's rather that the compiler can leave out the empty loop altogether.

这篇关于使用&lt; ctime&gt;进行基准测试和指令重新排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆