我开始在 VS2010 中使用并行模式库,该应用程序给了我预期的结果,但是当我对调试版本和发布版本进行基准测试时,我在发布版本中得到奇怪的执行时间,如下调试版本:“顺序持续时间:1014”“并行持续时间:437 ”发布版本“连续持续时间:31”“并行持续时间:484”
这是我的应用程序代码
double DoWork(int workload)
{
double result=0;
for(int i =0 ; i < workload;i++)
{
result +=sqrt((double)i * 4*3) + i* i;
}
return result;
}
vector<double> Seqential()
{
vector<double> results(100);
for(int i = 0 ; i <100 ; i++)
{
results[i] = DoWork(1000000);
}
return results;
}
vector<double> Parallel()
{
vector<double> results(100);
parallel_for(0,(int)100,1,[&results](int i)
{
results[i] = DoWork(1000000);
});
return results;
}
double Sum(const vector<double>& results)
{
double result =0;
for(int i = 0 ; i < results.size();i++)
result += results[i];
return result;
}
int main()
{
DWORD start = GetTickCount();
vector<double> results = Seqential();
DWORD duration = GetTickCount() - start;
cout<<"Sequential Duration : "<<duration <<" Result : " <<Sum(results) << endl;
start = GetTickCount();
results = Parallel();
duration = GetTickCount() - start;
cout<<"Prallel Duration : "<<duration <<" Result : " <<Sum(results) << endl;
system("PAUSE");
return 0;
}