这个是没使用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;
}
#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;
}
解决方案 »
- 编码问题
- 疑难问题,怎样拖拽音频,视频,图片等文件到一个自己开发的应用程序中?
- 求助: 大文件单线程跨网传输解决方案。
- 关于Oullook2003插件的问题
- 急求
- 高手请进,有关于两个库LIBCD.LIB和LIBCMTD.LIB冲突的问题,有时是warning,弄不好就是Error,如何解决?
- 各位大哥大姐,有没有可以使用的扫描仪控件?急,有源代码的更好,跪谢!
- 哪一个网站卖计算机书八折,且上海外环线内和交大免费送货的?
- 怎么显示各种格式的图片 JPG GIF等等
- 在执行ShellExecute()启动Outlook,怎样自动粘上某个文件作为附件??
- PIC空间内部空间的刷新问题
- 只有窗口重绘后,图形才会改变?
大概你是每个核心都算了一遍。
#pragma omp parallel 后面写公有或似有变量
{
#pragma omp for
for(...)
{
...
}
}