Armin Meisterhirn
Armin Meisterhirn

Reputation: 801

Eigen Library Speed Up Matrix Initialization

Is there a way to speed up the running time of the following code?

Matrix<double, 10, Dynamic> A(10,n);
A << a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;

Here, n is only known at runtime, and a1, a2, and so on are vectors of length n. I've tried to approximate the max size of the matrix and then to use double, 10, Dynamic, 0, 10, 10000 but this did not give any increase in speed.

Upvotes: 1

Views: 1369

Answers (1)

ggael
ggael

Reputation: 29205

If you use a RowMajor matrix for A, then the copies will be much faster thanks to better cache coherence and vectorization:

Matrix<double,10,Dynamic,RowMajor> A(10,n);

However, this might also slow down other operations.

Finally, make sure you compiled with optimizations on (e.g., -O2 with gcc), and it might be slightly faster to avoid the comma initializer syntax with:

A.row(i) = a_i;

(not sure because that depends on the compiler, but that's worth trying if that copy is a bottleneck)

Upvotes: 4

Related Questions