Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4110674
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:57:52+00:00 2026-05-20T21:57:52+00:00

Does anyone know if there is a list of what a compiler do to

  • 0

Does anyone know if there is a list of what a compiler do to optimize a source code? I prefer GCC as example.

I want to know what a programmer should do with the code to get good optimization and help the compiler to optimize it. Some optimizations by programmer may avoid the compiler to do better optimizations.

Examples:

replace
for (int i = 0; i < n - 1; i++ )
by
int n2 = n - 1;
for (int i = 0; i < n2; i++ )


for (int i = 0; i < n/2; i++ )
by
int n2 = n/2;
for (int i = 0; i < n2; i++ )



for (int i = 0; i < obj.calc_value(); i++ ) //calc_value() will return the same result with obj remaining unchanged.
by
int y = obj.calc_value()
for (int i = 0; i < y; i++ )

It is important to keep the code simple to read and understand.

Thanks

Edit:

Other examples:

  • Inline functions
  • Remove recursion
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T21:57:53+00:00Added an answer on May 20, 2026 at 9:57 pm

    Seriously, just leave that up to the compiler. I’ve seen the code that gcc outputs at its “insane” -O3 level and it’s proof positive that the people who wrote those optimisation engines are either aliens or from a substantially distant future time.

    I’ve yet to see a situation where register or inline made an appreciable difference in performance of my code. That doesn’t mean it won’t, just that the compiler writers know far more tricks than us mere mortals when it comes to extracting the last ounce of performance from the processor.

    As far as optimisation goes, it should only be done where there is a real problem. That means profiling code and discovering bottlenecks but, more importantly, not optimising an operation that is not deemed slow in context. There zero difference to a user whether a one-shot operation takes a tenth of a second or a hundredth.

    And sometimes, optimisation for readability is the best one you can do 🙂


    As an aside, this is just one of the nifty tricks gcc does for you. Consider the following code which is supposed to calculate the factorial and return it:

    static int fact (unsigned int n) {
        if (n == 0) return 1;
        return n * fact (n-1);
    }
    int main (void) {
        return fact (6);
    }
    

    This compiles to (at -O3):

    main: pushl    %ebp            ; stack frame setup.
          movl     $720, %eax      ; just load 720 (6!) into eax.
          movl     %esp, %ebp      ; stack frame
          popl     %ebp            ;   tear-down.
          ret                      ; and return.
    

    That’s right, gcc just works it all out at compile-time and turns the whole thing into the equivalent of:

    int main (void) { return 720; }
    

    Contrast this with the -O0 (naive) version:

    main:  pushl   %ebp               ; stack 
           movl    %esp, %ebp         ;   frame
           andl    $-16, %esp         ;   set
           subl    $16, %esp          ;   up.
           movl    $6, (%esp)         ; pass 6 as parameter.
           call    fact               ; call factorial function.
           leave                      ; stack frame tear down.
           ret                        ; and exit.
    
    fact:  pushl   %ebp               ; stack
           movl    %esp, %ebp         ;   frame
           subl    $24, %esp          ;   set up.
           cmpl    $0, 8(%ebp)        ; passed param zero?
           jne     .L2                ; no, keep going.
           movl    $1, %eax           ; yes, set return to 1.
           jmp     .L3                ; goto return bit.
    
    .L2:   movl    8(%ebp), %eax      ; get parameter.
           subl    $1, %eax           ; decrement.
           movl    %eax, (%esp)       ; pass that value to next level down.
           call    fact               ; call factorial function.
           imull   8(%ebp), %eax      ; multiply return value by passed param.
    
    .L3:   leave                      ; stack frame tear down.
           ret                        ; and exit.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know if there's a way to automatically expand a list in Python,
Does anyone know if there is a way to get a list of controls
Does anyone know if there is a way to get a list of all
Does anyone know if there is a CheckBox List for a Drop Down List
Does anyone know the full list of C# compiler number literal modifiers? By default
Does anyone know if there's a way to run automatically in shell a list
Does anyone know the location of silverlight 5 feature request list? If there isnt
Does anyone know there have any other way that (by not using json_encode and
Does anyone know how to read a x.properties file in Maven. I know there
Does anyone know whether there's a way to mock Entity Data Provider so Unit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.