<strong><span style="font-size:24px;">//测试VS2010下开启OpenPM并行提速</span></strong>
<span style="white-space:pre"> </span>LARGE_INTEGER tmp,time1,time2;
long freq,costTime;
QueryPerformanceFrequency(&tmp);
freq = tmp.QuadPart;
int k = 0;
CString str;
int kk[100];
QueryPerformanceCounter(&time1);
for (int i = 0;i<100;++i)
{
kk[k] = i;
k++;
Sleep(10);
}
QueryPerformanceCounter(&time2);
costTime = (long)((time2.QuadPart - time1.QuadPart) * 1000 / freq);
如上代码运行时间costTime = 999。
开启OpenMP并行提速:
1、项目---属性---配置属性---C/C++---语言---OpenMP支持---是(如下图)
2、循环前添加
<span style="color:#ff0000;">#pragma omp parallel for</span>
如下图
LARGE_INTEGER tmp,time1,time2;
long freq,costTime;
QueryPerformanceFrequency(&tmp);
freq = tmp.QuadPart;
int k = 0;
CString str;
int kk[100];
QueryPerformanceCounter(&time1);
<pre name="code" class="cpp"><span style="white-space:pre"> </span><span style="color:#ff0000;">#pragma omp parallel for</span>
for (int i = 0;i<100;++i){kk[k] = i;k++;Sleep(10);}QueryPerformanceCounter(&time2);costTime = (long)((time2.QuadPart - time1.QuadPart) * 1000 / freq);
如上代码运行时间costTime = 135左右。
我的机器是8核的,costTime 约等于 ceil(100/8)×10 = 130
测试可显著提速,可看下运行完后kk数组中数据,可看出二者间的区别。
不开启OpenMP 是按顺序0-99;开启后数据混乱,且每次运行都不一样。