Is there a way to speed up the creating of MatrixXd full of zeroes ? Indeed, consider this code
#include <vector>
#include <iostream>
#include <chrono>
#include <ctime>
#include <Eigen/Core>
int main() {
std::vector<void*> results;
results.reserve(2000);
int n = 200;
auto t0 = std::chrono::system_clock::now();
for(int i = 0; i < 1000; i++) {
Eigen::MatrixXd* A = new Eigen::MatrixXd(n,n);
A->setZero();
results.push_back(A);
}
auto t1 = std::chrono::system_clock::now();
for(int i = 0; i < 1000; i++) {
double* A = (double*)calloc(n * n, sizeof(double));
results.push_back(A);
}
auto t2 = std::chrono::system_clock::now();
std::chrono::duration<double> t01 = t1-t0;
std::chrono::duration<double> t12 = t2-t1;
printf("Eigen+setZero %e calloc %e size %lu\n", t01.count(), t12.count(), results.size());
}
resulting on (on my laptop)
./a.out
Eigen+setZero 5.440070e-01 calloc 1.527000e-03 size 2000
The matrices are built using Eigen::MatrixXd(n,n) followed by a setZero.
Looking at the timings in more details, the setZero is taking almost all the time (the new being super fast). Any way to speedup this ?
Ideally I would like to get down to what's possible with using just calloc. A memset doesn't really help, it's as slow as setZero. Finally, I would like to have MatrixXd, so the Map route isn't ideal.