5

In a project I'm working on, there's a quadruple-nested for loop in a large file I'm trying to optimize I think would benefit from a compiler unroll with -funroll-all-loops. However, when I add this flag to the compiler, it unrolls the other loops the rest of the file and makes the overall program run more slowly. Is there a way (possibly via a #pragma) to apply compiler flags only to certain functions in the file instead of the entire file?

Thanks in advance.

2 Answers2

5

The GCC function attribute optimize can be used to set an optimization option for a single function:

void foo(int bar) __attribute__((optimize ("unroll-all-loops")))
{
}
caf
  • 233,326
  • 40
  • 323
  • 462
1

I would suggest moving that particular function to a separate .c file that can be compiled with the extra options you want to use. This may necessitate creating a "foo_private.h" style header to share between the existing .c file and the new one that allows them to share any variables that were declared as static in the original .c file

Ryan
  • 185
  • 11