//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();
}