是std :: packaged_task真贵吗?

时间:2013-10-05 21:49:39

标签: performance gcc c++11 packaged-task

我对在Opensuse Linux上使用gcc 4.7.2的以下代码的结果感到惊讶:

#include <cmath>
#include <chrono>
#include <cstdlib>
#include <vector>
#include <chrono>
#include <iostream>
#include <future>

int main(void)
{
  const long N = 10*1000*1000;
  std::vector<double> array(N);
  for (auto& i : array)
    i = rand()/333.;

  std::chrono::time_point<std::chrono::system_clock> start, end;
  start = std::chrono::system_clock::now();
  for (auto& i : array)
    pow(i,i);
  end = std::chrono::system_clock::now();
  std::chrono::duration<double> elapsed_seconds = end-start;
  std::cout << "elapsed time: " << elapsed_seconds.count() << "s\n";

  start = std::chrono::system_clock::now();
  for (auto& i : array)
    std::packaged_task<double(double,double)> myTask(pow);
  elapsed_seconds = std::chrono::system_clock::now()-start;
  std::cout << "elapsed time: " << elapsed_seconds.count() << "s\n";

  start = std::chrono::system_clock::now();
  for (auto& i : array)
    std::packaged_task<double()> myTask(std::bind(pow,i,i));
  elapsed_seconds = std::chrono::system_clock::now()-start;
  std::cout << "elapsed time: " << elapsed_seconds.count() << "s\n";

  return 0;
}

结果看起来像这样(并且在运行中相当一致):

elapsed time: 0.694315s
elapsed time: 6.49907s
elapsed time: 8.42619s

如果我正确地解释了结果,只需创建一个std::packaged_task(甚至不执行它或存储它的参数)已经比executing {{贵十倍了1}}。这是一个有效的结论吗?

为什么会这样?

这是偶然的gcc具体吗?

0 个答案:

没有答案