Author:DriverMonkey
Mail:bookwore.peng@hotmail.com
Phone:18575593141
测试环境:
Linux version 4.15.0-33-generic
gcc version 5.4.0
Ubuntu 5.4.0-6ubuntu1~16.04.10
四核处理器
测试代码:
//how to compile --- g++ openmp.cpp -fopenmp
#include <iostream>
using namespace std;
float get_time()
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
float return_v = float(ts.tv_sec) + float(ts.tv_nsec / (1e9));
cout<<"get_time = "<<return_v<<endl;
return return_v;
}
int main()
{
#ifndef _OPENMP
cout<<"Sorry, the platform don't support openmp."<<endl;
#endif
int const count = 100*1024*1024;
static int temp[count];
float t1 = get_time();
#pragma omp parallel for
for(int i = 0; i < count; i++)
{
temp[i] =i * 2;
temp[i] += i;
}
float t2 = get_time();
cout<<"time = "<<t2 - t1<<endl;
}
测试结果:
打开多核加速 代码运行时间 --- time = 0.125
未开多核加速运行时间 --- time = 0.5
结论:
打开多核加速是未开多核加速的 4 倍,跟当前使用的是四核处理器相吻合

本文通过在四核处理器上运行的代码对比了开启和关闭多核加速的情况,结果显示,开启多核加速后的代码运行时间是未开启状态下的四分之一,验证了多核并行处理的效率提升。
1191

被折叠的 条评论
为什么被折叠?



