Reputation: 437
I found that calculation of substracting two Eigen::Vector3f is slower than the substracting two strucutre.
I call 10000000 times subtraction between Eigen::Vector3f and self-defined structure. I found the speed of self-defined structure is 20 times faster then Eigen::Vector3f.
Here is my test code.
#include<Eigen/Eigen>
#include<iostream>
#include "timer.h"
struct Point {
float x, y, z;
Point Add(const Point& point) const {
Point result = {
x + point.x,
y + point.y,
z + point.z
};
return result;
}
};
Eigen::Vector3f sub_eigen(const Eigen::Vector3f& vector_a,
const Eigen::Vector3f& vector_b) {
Eigen::Vector3f result;
result = vector_a - vector_b;
return result;
};
Point sub_point(const Point& point_a,
const Point& point_b) {
Point result = point_a.Add(point_b);
return result;
}
int main() {
Eigen::Vector3f source_eigen(1.f, 1.f, 1.f);
Eigen::Vector3f dest_eigen(1.f, 1.f, 1.f);
Point point_a = {1.f, 1.f, 1.f};
Point point_b = {1.f, 1.f, 1.f};
int num_tests = 10000000;
std::cout << "Caculate time:" << num_tests << "\n";
Timer timer;
timer.Tic();
for (int i = 0; i < num_tests; i++) {
Eigen::Vector3f result_eigen =
sub_eigen(source_eigen, dest_eigen);
}
timer.Toc();
std::cout << "Time[Eigen]:" << timer.TimeElapsed() << "\n";
timer.Tic();
for (int i = 0; i < num_tests; i++) {
Point result_point =
sub_point(point_a, point_b);
}
timer.Toc();
std::cout << "Time[Point]:" << timer.TimeElapsed() << "\n";
return 0;
}
The result is following:
Caculate time:10000000
Time[Eigen]:3.3389
Time[Point]:0.163778
As you can see, the speed of Eigen::Vector3f is nearly 20 times slower than self-defined structre.
Do I use eigen in wrong way?
Upvotes: 0
Views: 802
Reputation: 2859
Eigen is very sensitive to optimization, for example on my system with -O0
:
Caculate time:10000000
Time[Eigen]:6.80348
Time[Point]:0.275657
But with -O3
:
Caculate time:10000000
Time[Eigen]:0.0128227
Time[Point]:0.0116121
Upvotes: 4