This forum has been archived. All content is frozen. Please use KDE Discuss instead.

A compilation and performance problem

Tags: None
(comma "," separated)
yc2986
Registered Member
Posts
2
Karma
0
Hello Forum,

I am a Python Numpy user in the past. Right now I would like to do the same numerical linear algebra calculation in C++ so I select Eigen.
When I was doing some performance test with Eigen, I find some interesting thing regard to compilation of the source code.

I am writing a simple sigmoid fuction in a eigentest.cpp source code:
Code: Select all
#include <iostream>
#include <Eigen/Dense>
#include <time.h>

using Eigen::MatrixXd;
using namespace std;

int main() {
   MatrixXd m = MatrixXd::Random(3000,3000);
   time_t start = clock();
   m = -m;
   MatrixXd sig = 1 / (1 + m.array().exp());
   cout << double(clock() - start) / CLOCKS_PER_SEC * 1000 << "ms" << endl;
   return 0;
}


I do the compilation using the simple command:
Code: Select all
g++ -I. eigentest.cpp -o eigentest


When I run the executable it returns an elapsed time of roughly 900ms. It is even slower than the Numpy.
So I decide to do some research until I find this post on stackoverflow.
http://stackoverflow.com/questions/24566920/eigen-matrix-vs-numpy-array-multiplication-performance

Someone mentioned to add the compilation option like this:
Code: Select all
g++ -I. eigentest.cpp -o eigentest -O2 -DNDEBUG


Then I ran the program again. This time I got 170ms elapsed time. This is much faster than python Numpy.

I would like to ask what did the -O2 and -DNDEBUG switch do in the compilation that boost the performance here?
Do I need to apply this switch every time in my future compilation to get the best performance out of Eigen library?

Thank you very much!
mazz
Registered Member
Posts
1
Karma
0
It is a g++ related question, not an eigen one.

https://gcc.gnu.org/onlinedocs/gcc/Opti ... ze-Options

ctrl+f for -o2
yc2986
Registered Member
Posts
2
Karma
0
mazz wrote:It is a g++ related question, not an eigen one.

https://gcc.gnu.org/onlinedocs/gcc/Opti ... ze-Options

ctrl+f for -o2


I think I should apply this optimization flag every time when compiling with g++.

Then what about compile on Windows platform?

According to the Configuring your project for optimal performance in this link: http://eigen.tuxfamily.org/index.php?title=Visual_Studio, I compiled the same code in visual studio 2010 professional using all the optimizer setting under Project property. This gave me an even worse result of 3500ms elapsed time.

I will try osx compile when I have chance.

Just a little bit confused on how to get best performance out of compiling on each platform.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Enabling compiler optimizations is indeed crucial in C++ to get rid of the deep function calls through inlining. -O2 is the minimal, -O3 is slightly better. This flag applies to both gcc and clang (default on OSX). For visual, the flag if /O2, or "release mode" through the interface.


Bookmarks



Who is online

Registered users: Bing [Bot], Evergrowing, Google [Bot], rockscient