why inline is faster than normal function call?

Discussion in 'C++' started by thomas, Nov 13, 2009.

  1. thomas

    thomas Guest

    any other reason except that inline inserts code directly to the
    caller while function call push parameters to the stack (which is time
    consuming)?
     
    thomas, Nov 13, 2009
    #1
    1. Advertising

  2. thomas

    SG Guest

    On 13 Nov., 17:07, thomas <> wrote:
    > any other reason except that inline inserts code directly to the
    > caller while function call push parameters to the stack (which
    > is time consuming)?


    A function call is a boundary the compiler's optimizer can't (easily)
    work across. If the code is inlined, this boundary is eliminated and
    the optimizer has more information available to improve performance.
     
    SG, Nov 13, 2009
    #2
    1. Advertising

  3. thomas wrote:
    > any other reason except that inline inserts code directly to the
    > caller while function call push parameters to the stack (which is time
    > consuming)?


    Uh... Yes, "time consuming" usually means "slower". Is this a trick
    question? If you compare the code generated to call a function versus
    to perform the same operations without calling a function, you will
    notice that the function calling code contains extra instructions, to
    execute which the CPU is bound to spend more time. Among those
    instructions are, as you mentioned, the copying of arguments where they
    are supposed to go (stack, registers), extracting them from that place
    once inside the function body, general preserving of some CPU registers,
    restoring them afterwards, and so on. Often with stack-based argument
    passing, functions in the beginning contain code to check the stack size
    or even integrity, which also uses up some time... If the body of the
    function is available to the compiler, the compiler can optimize more
    aggressively, thus saving more time. Probably there are other things,
    although we're already too far into hardware-specific area...

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 13, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,222
    Smokey Grindel
    Dec 2, 2006
  2. TGOS
    Replies:
    3
    Views:
    401
    Kevin Bracey
    Feb 28, 2005
  3. Replies:
    7
    Views:
    462
  4. Leo Jay
    Replies:
    1
    Views:
    304
  5. THAKUR PRASHANT SINGH

    Class Function call vs Normal Function call

    THAKUR PRASHANT SINGH, Feb 26, 2010, in forum: Ruby
    Replies:
    7
    Views:
    218
    THAKUR PRASHANT SINGH
    Feb 27, 2010
Loading...

Share This Page