这取决于您投入多少数据。
在下面的示例中,4096 个条目将通过一个简单的循环更快。但是对于 1000*4096 条目,并行版本更快。
因此,您的 20-30% 改进的结果可能只是在有问题的硬件的范围之间。
这是我使用的测试程序。
第一次运行是简单循环,第二次来自问题,第三次使用std::launch::async
.
Plain From With
loop question launch::async
First Second Third
166 1067 607
166 614 434
166 523 509
265993 94633 66231
182981 60594 69537
237767 65731 57256
这是现场结果。
#include <vector>
#include <thread>
#include <future>
#include <complex>
#include <string>
#include <iostream>
#include <chrono>
#include <random>
#include <ratio>
float get_random()
{
static std::default_random_engine e;
static std::uniform_real_distribution<> dis(0,1); // rage 0 - 1
return static_cast<float>(dis(e));
}
void do_tests(float val1, float val2, float val3, float val4, int multiplier)
{
{
std::vector<std::complex<float>> Input1(4096*multiplier,std::complex<float>{val1,val2});
std::vector<std::complex<float>> Input2(4096*multiplier,std::complex<float>{val3,val4});
std::complex<float> ResSum(0,0);
auto start{std::chrono::high_resolution_clock::now()};
size_t size = Input1.size();
for (int i=0; i<size; ++i) {
ResSum += Input1[i]*Input2[i];
}
auto end{std::chrono::high_resolution_clock::now()};
auto time_used{end-start};
std::cout << std::chrono::duration_cast<std::chrono::microseconds>(time_used).count() << "\t\t";
}
{
std::vector<std::complex<float>> Input1(4096*multiplier,std::complex<float>{val1,val2});
std::vector<std::complex<float>> Input2(4096*multiplier,std::complex<float>{val3,val4});
std::complex<float> ResSum(0,0);
auto start{std::chrono::high_resolution_clock::now()};
size_t size = Input1.size()/8;
std::vector<std::future<std::complex<float>>> futures;
futures.reserve(8);
for (int i = 0; i<8; ++i) {
futures.push_back(
std::async(
[](std::complex<float>* pos,std::complex<float>*pos2,size_t siz) {
std::complex<float> resum(0,0);
for (int i = 0; i < siz; ++i)
resum += pos[i]*pos2[i];
return resum;
}
,&Input1[i*size],&Input2[i*size],size
)
);
}
for (int i = 0; i < futures.size(); ++i)
ResSum += futures.at(i).get();
auto end{std::chrono::high_resolution_clock::now()};
auto time_used{end-start};
std::cout << std::chrono::duration_cast<std::chrono::microseconds>(time_used).count() << "\t\t";
}
{
std::vector<std::complex<float>> Input1(4096*multiplier,std::complex<float>{val1,val2});
std::vector<std::complex<float>> Input2(4096*multiplier,std::complex<float>{val3,val4});
std::complex<float> ResSum(0,0);
auto start{std::chrono::high_resolution_clock::now()};
size_t size = Input1.size()/8;
std::vector<std::future<std::complex<float>>> futures;
futures.reserve(8);
for (int i = 0; i<8; ++i) {
futures.push_back(
std::async(std::launch::async,
[](std::complex<float>* pos,std::complex<float>*pos2,size_t siz) {
std::complex<float> resum(0,0);
for (int i = 0; i < siz; ++i)
resum += pos[i]*pos2[i];
return resum;
}
,&Input1[i*size],&Input2[i*size],size
)
);
}
for (int i = 0; i < futures.size(); ++i)
ResSum += futures.at(i).get();
auto end{std::chrono::high_resolution_clock::now()};
auto time_used{end-start};
std::cout << std::chrono::duration_cast<std::chrono::microseconds>(time_used).count() << "\t\t";
}
std::cout << '\n';
}
int main()
{
float val1{get_random()};
float val2{get_random()};
float val3{get_random()};
float val4{get_random()};
std::cout << "First\t\tSecond\t\tThird\n";
do_tests(val1, val2, val3, val4, 1);
do_tests(val1, val2, val3, val4, 1);
do_tests(val1, val2, val3, val4, 1);
do_tests(val1, val2, val3, val4, 1000);
do_tests(val1, val2, val3, val4, 1000);
do_tests(val1, val2, val3, val4, 1000);
}