双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

OpenMp并行计算,为嘛双核没单核快,还比单核慢三倍

4lan 4lan 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
这个是没使用OpenMp的代码:50万*5000*次乘法运算 cpu占用50%也就是一个核心,耗费大概十秒

#include "stdafx.h"
#include <Windows.h>


int _tmain(int argc, _TCHAR* argv[])
{
unsigned long ticks1,ticks2;
ticks1 = GetTickCount();

unsigned long a=1;

for (int i = 1; i < 500000; i++)
{
for (int i = 1; i < 5000; i++)
{
a=1;
a=a*i;
}
}

printf("%d\n",a);
ticks2 = GetTickCount();

ticks1=ticks2-ticks1;
printf("程序经过毫秒数:%d",ticks1);
getchar();
return 0;
}

这个是使用OpenMp的运算。

添加omp文件头

将 Project 的Properties中C/C++里Language的OpenMP Support开启

在要使用openmp的地方 添加 #pragma omp parallel

结果:cpu占用100%两个核心,但是耗时30秒。。。。。。。搞不懂呀

#include "stdafx.h"
#include <Windows.h>
#include <omp.h>


int _tmain(int argc, _TCHAR* argv[])
{
unsigned long ticks1,ticks2;
ticks1 = GetTickCount();

unsigned long a=1;
#pragma omp parallel for
for (int i = 1; i < 500000; i++)
{
for (int i = 1; i < 5000; i++)
{
a=1;
a=a*i;
}
}

printf("%d\n",a);
ticks2 = GetTickCount();

ticks1=ticks2-ticks1;
printf("程序经过毫秒数:%d",ticks1);
getchar();
return 0;
}
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
4lan
进程/线程/DLL 15,466 成员 49,169 内容