compilation parameters, code optimization

Hi all,

I implemented an application, through using c++ and compiled it with g++.
At first, what I did is (@ compilation):
g++ calcBacon.C -o test -DDEBUG

after I ran my application it took almost 120 sec. to finish its execution

when I compiled with optimization parameters, execution time dramatically changed
what I did was:
g++ calcBacon.C -o test -O3 -DDEBUG

after this compilation overall execution took 35 sec. to finish.
It is a big difference (~85secs)...

My questions:
1) Is it related with my implementation? I mean, is my implementation so bad, therefore compiler optimization responds very good to my code? (is this the explanation?).
2) Do you know any other optimization parameter like this? Could you please explain in what cases they work good?

Thanks in advance

  1. The fact that the compiler was able to optimise your code well does not mean that it was badly written, just that the optimiser is doing its job. If code has been written in a way that helps the optimiser do its job, then that's good, but "hand-optimised" code tends to become unmaintainable gibberish. Concentrate more on finding better algorithms and writing clear code.

  2. GCC, like any decent compiler, has dozens of individual optimisation flags; they are fully described in the manual. O3, O2 and so on are short-hand ways of turning a whole bunch of them on. You can experiment with fine-tuning the optimisation if you need to or are curious, but this can be time consuming, so you had better be sure it's worth the effort. Most people just switch on -O2 or O3 and leave it at that.

Thanks for the answers..

I also wonder if optimized codes are stable. Can they produce unexpected outputs or failures?

According to below article, they might cause some unexpected errors but it is old (2003):
http://www.linuxfromscratch.org/hints/downloads/files/optimization.txt

spirtle gave you geat advice - usually 90% of bottlenecks are caused by algorithm choice. Some others unavoidable...

Do you know how to profile code?
compile

g++ -g -p mycode.cpp -o test

Run the code, then try

gprof test

This will show the time spent in each function - so you can locate the botteleneck.
Read the info page or man page for gprof.

By the way - it appears like you are compiling C code with a c++ compiler? Is that the case? If so, use gcc, not g++

I was implementing a C++ code. I don't know why but the guys who wrote in C++ here prefer .C (capital letter) instead of .cpp.

By the way, I tried to profile my code as you mentioned, however I couldn't make it. I think if the application gets some parameters from outside (like ./myexecutable file.txt) then it fails.

Thanks for the information though, I really appreciate that.

System: Ubuntu Hardy

It looks like recompiling the application with -p probably broke it. IMO, profiling with gprof is almost always a last resort as it is so intrusive that it may skew your profile.

This may be an unwanted advert, but you can use the statistical profiler I am working on now - Zoom. It does calltrees and side by side source and asm, which I believe are huge value-adds.

If you're only want open source solutions, search for oprofile and sysprof.