直接新建win32工程,添加C++代码后,在属性语言修改“支持openMP”:
#include "stdafx.h"
#include<omp.h>
#include <iostream>
using namespace std;
//循环测试函数
void test()
{
for(inti=0;i<10000;i++)
{
}
}
int _tmain(int argc, _TCHAR* argv[])
{
cout<<"这是一个并行测试程序!\n";
doublestart = omp_get_wtime( );//获取起始时间
#pragma ompparallel for
for(inti = 0; i < 10000; i++)
{
test();
}
doubleend = omp_get_wtime( );
cout<<"计算耗时为:"<<end -start<<"\n";
cin>>end;
return0;
}
并行程序:
//#include "stdafx.h"
#include<omp.h>
#include<iostream>
using namespace std;
//循环测试函数
void test()
{
for(int i=0;i<10000;i++)
{
}
}
int main()
{
cout<<"这是一个串行测试程序!\n";
double start = omp_get_wtime( );//获取起始时间
for(int i = 0; i < 10000; i++)
{
test();
}
double end = omp_get_wtime( );
cout<<"计算耗时为:"<<end -start<<"\n";
cin>>end;
return 0;
}
10000*10000啊,真是不做不知道啊,并行原来如此给力!!