【发布时间】:2018-12-06 15:50:33
【问题描述】:
当我需要将两个复数向量相乘时,我想比较犰狳的性能。我写了一个简单的测试来计算处理时间。乘法以两种方式实现:Armadillo 元素乘法和 std::vector 上的简单 for 循环。以下是测试源:
#include <iostream>
#include <armadillo>
#include <stdlib.h>
using namespace std;
using namespace arma;
#include <complex>
#include <chrono>
using namespace std::chrono;
#define VEC_SIZE 204800
main(int argc, char** argv) {
const int iterations = 1000;
cout << "Armadillo version: " << arma_version::as_string() << endl;
//duration<double> lib_cnt, vec_cnt;
uint32_t lib_cnt = 0, vec_cnt = 0;
for (int it = 0; it < iterations; it++) {
// init input vectors
std::vector<complex<float>> vf1(VEC_SIZE);
std::fill(vf1.begin(), vf1.end(), complex<float>(4., 6.));
std::vector<complex<float>> vf2(VEC_SIZE);
std::fill(vf2.begin(), vf2.end(), 5.);
std::vector<complex<float>> vf_res(VEC_SIZE);
// init arma vectors
Col<complex<float>> vec1(vf1);
Col<complex<float>> vec2(vf2);
// time for loop duration
auto t0 = high_resolution_clock::now();
for (int vec_idx = 0; vec_idx < VEC_SIZE; vec_idx++) {
vf_res[vec_idx] = vf1[vec_idx] * vf2[vec_idx];
}
auto t1 = high_resolution_clock::now();
vec_cnt += duration_cast<milliseconds>(t1 - t0).count();
for (int vec_idx = 0; vec_idx < VEC_SIZE; vec_idx++) {
complex<float> s = vf_res[vec_idx];
}
Col<complex<float>> mul_res(VEC_SIZE);
// time arma element wise duration
t0 = high_resolution_clock::now();
mul_res = vec1 % vec2;
t1 = high_resolution_clock::now();
lib_cnt += duration_cast<milliseconds>(t1 - t0).count();
}
cout << "for loop time " << vec_cnt << " msec\n";
cout << "arma time " << lib_cnt << " msec\n";
return 0;
}
结果如下:
$ g++ example1.cpp -o example1 -O2 -larmadillo
$ ./example1
Armadillo version: 9.200.5 (Carpe Noctem)
for loop time 2060 msec
arma time 3049 msec
我希望犰狳可以比简单的循环更快地繁殖。还是我错了?是否期望 for 循环将两个向量相乘更快?
【问题讨论】:
标签: stl linear-algebra armadillo