avr-gcc compiler flags question

Hi all,

I've been experimenting with compiler optimization flags... specifically "-Os" (optimize for smallest size - the Arduino default) and "-Ofast" which attempts to generate the fastest code without regard to code size.

Well holy smokes! Using the -Ofast flag makes my code run almost 2 times faster. Of course, it gets almost 2 times larger, but it fits with tons of room to spare, so who cares?

I haven't done much testing so far, but code that's sped up is at least:

  • pinMode
  • digitalRead and Write
  • analogRead
  • direct stuff (like PORTB |= _BV(3))
  • reading data out of PROGMEM

I'm very skeptical... when something seems too good to be true, it usually is.

It SEEMS that my code runs just fine compiled with the -Ofast flag... does anyone know any reason why I SHOULDN'T use it (please don't mention code size - that's obvious).

Thanks!

-- Roger

I would be curious to see how -Ofast differs from -O3?

Per the GNU GCC documentation:
-Ofast: Disregards strict standards compliance and enables all -O3 optimizations. It also enables optimizations that are not valid for all standard-compliant programs. It turns on -ffast-math.

code that's sped up is at least:

  • direct stuff (like PORTB |= _BV(3))

That's difficult to believe, since things like "PORTB |= 8" compile to single instructions even with -Os.

Show us the disassembly...

westfw:
That's difficult to believe, since things like "PORTB |= 8" compile to single instructions even with -Os.

Show us the disassembly...

Sorry... kinda misleading. I was mentioning the things that I was using in the test sketch I used to compare speeds (a sketch to run a KS-108 compatible VFD through a lot of tests and my Noritake VFD library to drive the VFD.

The library of course has a lot of direct port accesses so I just mentioned them without thinking.