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?
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.
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.
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.
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.