2

I tried to optimize C code with gcc. If I use -O1 option will have a better running time but if i use equivalent

  -fauto-inc-dec 
  -fcompare-elim 
  -fcprop-registers 
  -fdce 
  -fdefer-pop 
  -fdelayed-branch 
  -fdse 
  -fguess-branch-probability 
  -fif-conversion2 
  -fif-conversion 
  -fipa-pure-const 
  -fipa-profile 
  -fipa-reference 
  -fmerge-constants
  -fsplit-wide-types 
  -ftree-bit-ccp 
  -ftree-builtin-call-dce 
  -ftree-ccp 
  -ftree-ch 
  -ftree-copyrename 
  -ftree-dce 
  -ftree-dominator-opts 
  -ftree-dse 
  -ftree-forwprop 
  -ftree-fre 
  -ftree-phiprop 
  -ftree-slsr 
  -ftree-sra 
  -ftree-pta 
  -ftree-ter 
  -funit-at-a-time

its like I didn't use any options flag. GCC version is 4.8.2. Please, who can explain me why? I just found that order of these flags matter but I didn't found which order is in -O1.

6
  • Do post the command line please Commented Apr 19, 2015 at 18:12
  • -fomit-frame-pointer may also be enabled. gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html Commented Apr 19, 2015 at 18:17
  • and why does it even matter? do you want to specifically exclude any of the flags? Commented Apr 19, 2015 at 18:17
  • 1
    gettimeofday(&tv1, NULL); func(); gettimeofday(&tv2, NULL); printf("%f msec\n", (double)(tv2.tv_usec - tv1.tv_usec) / 1000 + (double)(tv2.tv_sec - tv1.tv_sec) * 1000); same imput every time. I'm not allowed to use -O1 optimization flag but any other flag Commented Apr 19, 2015 at 18:18
  • do you just call the function once? you should consider calling it in a loop (e.g. 10k times) to get an average value Commented Apr 19, 2015 at 18:19

1 Answer 1

9

According to the documentation,

Most optimizations are only enabled if an -O level is set on the command line. Otherwise they are disabled, even if individual optimization flags are specified.

https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html (emphasis mine)


When it continues to list the specific flags you can set, it notes:

You can use the following flags in the rare cases when “fine-tuning” of optimizations to be performed is desired.

Which makes it pretty clear that they don't expect you to make an entirely custom set of optimisation options, but rather choose the general level which best fits your scenario (which will vary depending on whether you are currently debugging, shipping, etc.), then fine-tune any flags which your code benefits from having on or off.

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

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.