5

I have a task to create optimized C++ source code and give it to friend for compilation. It means, that I do not control the final compilation, I just write the source code of C++ program.

I know, that a can make optimization during compilation with -O1 (and -O2 and others) options of GCC. But how can I get this optimized source code instead of compiled program? I am not able to configure parameters of my friend's compiler, that is why I need to make a good source on my side.

1
  • 1
    While you can write "optimized" source code, thinking about things like the cache and fine-tuning algorithms (where mathematical background might help), it's hard to write optimal code if you can't run profiling on the target system to find your bottlenecks. Commented Dec 19, 2011 at 8:05

3 Answers 3

5

The optimizations performed by GCC are low level, that means you won't get C++ code again but assembly code in best case. But you won't be able to convert it or something.

In sum: Optimize the source code on code level, not on object level.

Sign up to request clarification or add additional context in comments.

Comments

3

You could ask GCC to dump its internal (Gimple, ...) representations, at various "stages". The middle-end of GCC is made of hundreds of passes, and you could ask GCC to dump them, with arguments like -fdump-tree-all or -fdump-gimple-all; beware that you can get hundreds of dump files for a single compilation!

However, GCC internal representations are quite low level, and you should not expect to understand them without reading a lot of material.

The dump options I am mentionning are mostly useful to those working inside GCC, or extending it thru plugins coded in C or extensions coded in MELT (a high-level domain specific language to extend GCC). I am not sure they will be very useful to your friend. However, they can be useful to make you understand that optimization passes do a lot of complex processing.

And don't forget that premature optimization is evil : you should first make your program run correctly, then benchmark and profile it, at last optimize the few parts worth of your efforts. You probably won't be able to write correct & efficient programs without testing and running them yourself, before giving them to your friend.

Comments

1

Easy - choose the best algorithm possible, let the rest be handled by the optimizer.

Optimizing the source code is different than optimizing the binary. You optimize the source code, the compiler will optimize the binary.

For anything more than algorithm choice, you'll need to do some profiling. Sure, there are practices that can speed up code speed, but some make the code less readable. Only optimize when you have to, and after you measure.

1 Comment

it's more like "you should try to optimize the algorithms, the compiler will try to optimize the binary"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.