4

Is there a way to optimize the GCC compiled code in term of cpu and memory using option flags? Using O3 rather than 01 does increase or decrease the amount of memory or cpu usage?

2
  • usually optimised code consumes more cpu (for less time). do you mean optimising for executable file size? that's the only optimisation in terms of memory consumption the compiler is doing for you. Commented Jun 15, 2015 at 15:49
  • You will have better luck decreasing the size of your code by writing it in a more "thrifty" style. There is only so much the compiler can do to optimize it, but it is up against the limitations of what you have written the code to do. Commented Jun 15, 2015 at 16:21

3 Answers 3

6

About memory usage:

  • -Os reduces the binary size of a program. It has limited effect on runtime memory usage (C/C++ memory allocation and deallocation is "manual").

    I say limited since tail recursion optimization can lower stack usage (this optimization will also be performed with -O2 / -O3).

  • The -flto (link time optimization) option can also lower binary size.

CPU usage:

Probably you should use -O2 and do not worry about it: if you're looking to save power / memory, the overall design of your application will have more effect than a compiler switch.

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

2 Comments

is there any previous work that studies the impact of compiler optimizations on the %CPU and Memory ? The link you gave me is interesting but I am trying to find some works related to mem and cpu. Thank you
-O3 -flto -march=native can help to let GCC use new instructions your CPU supports but others don't. Also -ffast-math can enable a lot of efficiency in FP code if it's not a correctness problem, or at least -fno-math-errno -fno-trapping-math if you still need strict FP.
1

You might try -Os which is like -O2 (good CPU speed) while simultaneously trying to reduce the binary size.

Check out the various optimizations here.

Comments

0

Code size optimizations are addressed above.

I'm only looking at CPU optimization. You can write really good/optimized code that has low processor utilization, and really bad/unoptimized code that maximizes CPU utilization.

So how do you most effectively use your processor?

  1. First, use a good optimizing compiler. I won't speak to GCC, but Intel and some other purchased compilers (e.g. PGI) are very good at optimization.
  2. Exploit the underlying hardware, such as vector instructions, FMA, registers, etc.
  3. Follow best practices for use of peripherals, such as cellular, wifi, gps, etc.
  4. Following best practices for SW design, such as latency hiding, avoid polling by using interrupts, use a thread pool if appropriate, etc

Good luck.

Comments

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.