Matlab code:
invD = inv(D);
Dew=2*invD-E;
D is 1000×1000 matrix of complex numbers. It inverts in 0,5 sec or less.
I tried several ways of porting this code to C++, using different approaches, but it is always slower than 10 seconds. What am I doing wrong? How possibly Matlab optimizes that code?
I did’t write this matlab code, I just have to port it. Sorry, I’m much better in coding that in math.
I am fairly sure you are using
Eigenincorrectly. I tried inverting a random 1000×1000 matrix with octave (free alternative to MATLAB, probably slightly less optimized) and with the following Eigen code. Octave takes about 1s, Eigen about 1.5s (I’m usingboost::timerinstead of your timing solution, but only because it’s less of a hassle):The first possible culprit I can think of are compiler options. Did you compile your C++ code with
-O2at least?There is still a possibility that your matrices have a special structure, so that numerical considerations make the Eigen algorithm slower in your particular case, but I would look elsewhere for the problem before considering that.