为什么我的tbb比串行还慢?

程序如下:

 #include "stdafx.h"
#include <iostream>
#include <tbb/task_scheduler_init.h>
#include <tbb/task.h>
#include "tbb/tick_count.h"
#include <windows.h>
using namespace std;
using namespace tbb;

struct printtask :task {
 printtask(int n) :m_n(n){}
 task* execute() {
  cout << m_n;
  return NULL;
 }
private:
 int m_n;
};

int main() {

//开始tbb执行
 tick_count t0, t1;
 t0 = tick_count::now();
 task *dummy = new(task::allocate_root()) empty_task;
 dummy->set_ref_count(10+1);
 for(int i=0; i<10; i++) {
  task* childtask = new(dummy->allocate_child()) printtask(i);
  dummy->spawn(*childtask);
 }
 dummy->wait_for_all();
 dummy->destroy(*dummy);
 t1 = tick_count::now();
 cout << endl << "tbb time: " << (t1-t0).seconds() << endl;

 

//开始串行执行
 t0 = tick_count::now();
 for (int i = 0; i < 10; i++) {
  cout << i;
 }
 cout << endl;
 t1 = tick_count::now();
 cout << "serial time: " << (t1-t0).seconds() << endl;
 return 0;
}

运行结果如下:

9081726354
tbb time: 0.00430087
0123456789
serial time: 0.000346368

tbb消耗的时间要多于串行10倍,我不知道为什么?

你可能感兴趣的:(struct,null,iostream)