19

I came through this line in project source code written before a function. I want to know, what is the use of it ?

#pragma GCC optimize ("O3")

void somefunction()
{
  ....
}

Requesting to explain every argument used in the directive.

Thanks and Regards.

jww
  • 97,681
  • 90
  • 411
  • 885
Madhu R
  • 345
  • 1
  • 4
  • 9
  • 1
    Please read gcc doc pragma section! – Klaus Nov 10 '17 at 11:50
  • `#pragma` is *always* implementation-defined. Refer to your [compiler documentation](https://gcc.gnu.org/onlinedocs/gcc-7.2.0/gcc/Function-Specific-Option-Pragmas.html#Function-Specific-Option-Pragmas). – DevSolar Nov 10 '17 at 11:51
  • 1
    @DevSolar: headline & flags says gcc :-) – Klaus Nov 10 '17 at 11:51
  • 1
    Requesting people to try simply searching for things instead of demanding a personally tailored explanation. – underscore_d Nov 10 '17 at 11:52
  • Possible duplicate of [C code optimization using #pragma GCC optimize](https://stackoverflow.com/questions/6720779/c-code-optimization-using-pragma-gcc-optimize) – jww Nov 10 '17 at 12:06

1 Answers1

18

Pragmas are implementation specific but, in this case (gcc), it sets the optimisation level to 3 (high), similar in effect to using -O3 on the command line.

Details on optimisation levels for gcc, and the individual flags that get set in response, can be found here.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 1
    This seems to be a bit subtle - it is *similar* to using `-O3` on the command line, but not *equivalent*. In particular, it doesn't seem to enable interprocedural optimizations like inlining. [Example on godbolt](https://godbolt.org/z/9dYTG9The) - actually using `-O3` causes `please_inline_me()` to be inlined, but `#pragma GCC optimize ("O3")` doesn't. – Nate Eldredge May 21 '21 at 16:35