Optimize for speed or size

Post Reply
jasonk
Posts: 100
Joined: Mon May 23, 2011 6:38 am
Location: Chandler, Arizona
Contact:

Optimize for speed or size

Post by jasonk » Sun Jun 05, 2011 9:56 pm

Currently, mpide is configured to call the compiler with the -O2 optimization option. The -O2 set of optimization usually results in a nice balance between code size and speed.

However, there are some instances where you want to sacrifice code size (use more FLASH memory) in order to get more speed. Luckily, Rick has introduced features in mpide that makes changing the default compiler options easy to change.

To change the optimization level:
  1. open the /hardware/pic32/platforms.txt file in your favorite text editor.
  2. Find the lines beginning with pic32.compiler.c.flags and pic32.compiler.cpp.flags. In those lines, you should see a compiler optimization option, -O0, -O1, -O2, -Os, or -O3.
  3. Change the optimization level option to the desired level, as listed below.
  4. Save platforms.txt
  5. Restart mpide.
Optimization levels
  • -O0 - Disable optimizations
  • -O1 - Reduce code size and execution time, without performing any optimizations that take a great deal of compilation time.
  • -O2 - Optimize even more. GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff. As compared to -O1, this option increases both compilation time and the performance of the generated code. (This is currently the default.)
  • -O3 - Optimize yet more. GCC optimizes for maximum performance at the expense of code size.
  • -Os - Optimize for size. -Os enables all -O2 optimizations that do not typically increase code size. It also performs further optimizations designed to reduce code size.
For example:

Code: Select all

pic32.compiler.c.flags=-O3::-c::-mno-smart-io::-ffunction-sections::-fdata-sections
pic32.compiler.cpp.flags=-O3::-c::-mno-smart-io::-ffunction-sections::-fdata-sections
If you really want aggressive performance for your sketch, you could also add the -funroll-loops option. Note that this option almost always increases code size but it may or may not increase performance.

For example:

Code: Select all

pic32.compiler.c.flags=-O3::-funroll-loops::-c::-mno-smart-io::-ffunction-sections::-fdata-sections
pic32.compiler.cpp.flags=-O3::-funroll-loops::-c::-mno-smart-io::-ffunction-sections::-fdata-sections
For more information on platforms.txt, see Rick's post here.
Jason Kajita
Microchip Technology
Follow me on Twitter

Post Reply