并行计算效率对比

4 篇文章 0 订阅
//Qt
#include <QtCore/QCoreApplication>
#include <QtConcurrent/QtConcurrent>
#include <QFuture>
#include <QList>
//C++
#include <iostream>
//OpenMP
#include <omp.h>
//tbb
#include <tbb/tbb.h>
//PPL
#include <ppl.h>

//测试各种并行运算方式的运行效率
//OpenMP使用教程:https://blog.csdn.net/dsif1995/article/details/50768970
//intel tbb下载页面:https://github.com/intel/tbb/releases
//tbb使用教程:https://blog.csdn.net/dwosion/article/details/72724371
//tbb/ppl基本用法:https://www.cnblogs.com/qicosmos/p/3517166.html
//QtConcurrent使用教程:https://www.fearlazy.com/index.php/post/95.html
class Point
{
public:
	Point(const float& x, const float& y, const float& z)
		:_x(x)
		, _y(y)
		, _z(z)
	{

	}
	float _x;
	float _y;
	float _z;
};

void Func(Point& point) {
	point._x += 1;
	point._y += 2;
}

int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);

	QList<Point> pointList;
	for (int i = 0; i < 1000; i++)
	{
		pointList.push_back(Point(0, 0, 0));
	}
	for (int k = 0; k < 10000; k++)
	{
		//方式一 传统for循环
		//方式二 为for循环开启OpenMP并行运算
		//一个for循环10000000点300ms
		//两个for循环1000*10000点300ms
		//使用OpenMP并行运算150ms
		//#pragma omp parallel for
		/*for (int j = 0; j < 10000000; j++)
		{
		pointList[j]._x += 1;
		pointList[j]._y += 2;
		}*/

		//方式三 QtConcurrent
		//一个for循环10000000点80ms
		//两个for循环1000*10000点7700ms
		//QFuture<void> f = QtConcurrent::map(pointList, Func);
		//f.waitForFinished();

		//方式四 intel tbb
		//intel tbb 10000000点360ms
		//intel tbb 1000*10000点360ms
		/*tbb::parallel_do(pointList.begin(), pointList.end(), [](Point& point)
		{
		point._x += 1;
		point._y += 2;
		});*/

		//方式五 microsoft ppl
		//microsoft ppl 10000000点80ms
		//microsoft ppl 1000*10000点100ms
		//它的效率与开启OpenMP并行运算接近
		concurrency::parallel_for_each(pointList.begin(), pointList.end(), [&](Point& point) {
			point._x += 1;
			point._y += 2;
		});
	}

	return a.exec();
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值