多核加速(OPENMP)测试

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

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 倍,跟当前使用的是四核处理器相吻合

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值