2,408
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<omp.h>
#include<ctime>
using namespace std;
int main()
{
double data[20] = { 0 };
clock_t t1, t2,t3;
t1 = t2 = t3=clock();
omp_set_num_threads(4);
int temp;
#pragma omp parallel for private(temp)
for (int i = 0; i < 4; i++)
{
temp = omp_get_thread_num();
for (int j = 0; j < 100000000; j++)
{
data[4 * temp] += 1;
data[4 * temp + 1] += 1;
data[4 * temp + 2] += 1;
data[4 * temp + 3] += 1;
data[4 * temp + 4] += 1;
}
}
t2 = clock();
cout << "多线程时间:" << (double)(t2 - t1) / CLOCKS_PER_SEC * 1000 << "ms\n";
for (int i = 0; i <20; i++)
{
for (int j = 0; j < 100000000; j++)
{
data[i] += 1;
}
}
t3 = clock();
cout <<"串行时间:"<< (double)(t3 - t2) / CLOCKS_PER_SEC * 1000 << "ms\n";
cin.get();
}