Macros and Executionspeed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ulf Kadner

    Macros and Executionspeed

    Hello!

    I have read, inline functions are, exactly like macros by the
    Preprocessor be implemented directly and therefore offer, due to a
    smaller Overheads a certain speed advantage. Did I understand it so
    correctly? Why is code implemented by the Preprocessor faster, and/or
    why this does not produce so much Overhead?

    I hope anyone can help me.

    PS.:
    I have read this in Bruce Eckel's "Thinking in C++ (9. Inline-Functions)"

    So long, Ulf
  • Karl Heinz Buchegger

    #2
    Re: Macros and Executionspeed

    Ulf Kadner wrote:[color=blue]
    >
    > Hello!
    >
    > I have read, inline functions are, exactly like macros by the
    > Preprocessor be implemented directly and therefore offer, due to a
    > smaller Overheads a certain speed advantage. Did I understand it so
    > correctly? Why is code implemented by the Preprocessor faster, and/or
    > why this does not produce so much Overhead?[/color]

    Think of the following:
    You have a function:

    double twice( double Argument )
    {
    return 2 * Argument;
    }

    In order to call that function, eg. like this

    int main()
    {
    double x;
    x = twice( 5.0 );
    }

    The code has to actually call that function. That means that information
    must be stored somewhere from where the call was made, in order to give
    the return a chance to find its way back. Also: A new variable 'Argument'
    must be created, intialized with the value 5 and destroyed when the function
    actually returns. That brings us to the return value. Somehow the return value
    (the result of 2 * Argument) must find its way back to the caller. All of this
    costs time.

    Now the very same thing, but without a function call:

    int main()
    {
    double x;
    x = 2 * 10.0;
    }

    No call is made, no variable needs to be created, no return value must find it's
    way back, no return address must be stored somewhere. It is easy to see, that this
    is usually way faster then the first version.

    Now say, that you don't want the actual computation in main. What options are there.
    The old, C way, was to use a macro:

    #define TWICE(X) ( 2 * (X) )
    int main()
    {
    double x;
    x = TWICE(5.0);
    }

    when the Preprocessor runs, it substitutes the macro with the text its stands for.
    Thus after the preprocessor has done its work, the following is feed to the actual
    compiler:

    int main()
    {
    double x;
    x = ( 2 * (5.0) );
    }

    and heureka (!), we are back to the fast version.

    Another method would be to use an inline function. This is somewhat similar
    in that the compiler (not the preprocessor) is able to replace the actual
    call of the function with the function body:

    inline double twice( double Argument )
    {
    return 2 * Argument;
    }

    int main()
    {
    double x;
    x = twice( 5.0 );
    }

    Now if the compilers grants your inline request, it internally will modify
    your program as if you had written:

    int main()
    {
    double x;

    {
    double Argument = 5.0;
    x = 2 * Argument;
    }
    }

    a few optimizing steps later, this will reduce to:

    int main()
    {
    double x;
    x = 2 * 5.0;
    }


    HTH

    --
    Karl Heinz Buchegger
    kbuchegg@gascad .at

    Comment

    • Imre Palik

      #3
      Re: Macros and Executionspeed

      Karl Heinz Buchegger <kbuchegg@gasca d.at> writes:
      [color=blue]
      > ...
      > Another method would be to use an inline function. This is somewhat similar
      > in that the compiler (not the preprocessor) is able to replace the actual
      > call of the function with the function body:[/color]

      Except that there are no in-out register arguments for C++ functions, but
      they are possible with macros. Not that it matters too much in the most
      cases though.

      ImRe

      Comment

      • Ulf Kadner

        #4
        Re: Macros and Executionspeed

        Karl Heinz Buchegger wrote:
        [color=blue]
        > Another method would be to use an inline function. This is somewhat similar
        > in that the compiler (not the preprocessor) is able to replace the actual
        > call of the function with the function body:[/color]

        Servus Karl Heinz!

        Thus I understood it wrongly! I thought always, the pre-processor also
        runs the Inline-functions.

        Thanks for your very usefull, detailed description!

        so long, Ulf

        Comment

        • msalters

          #5
          Re: Macros and Executionspeed


          Imre Palik schreef:
          [color=blue]
          > Karl Heinz Buchegger <kbuchegg@gasca d.at> writes:
          >[color=green]
          > > ...
          > > Another method would be to use an inline function. This is somewhat similar
          > > in that the compiler (not the preprocessor) is able to replace the actual
          > > call of the function with the function body:[/color]
          >
          > Except that there are no in-out register arguments for C++ functions, but
          > they are possible with macros. Not that it matters too much in the most
          > cases though.[/color]

          Wrong. With inline functions, both in and out arguments can be passed
          in registers. The reason is that the entire function implementation
          is visible to the compiler, so it can allocate registers for any
          variable (including arguments).

          HTH,
          Michiel Salters

          Comment

          • Imre Palik

            #6
            Re: Macros and Executionspeed

            "msalters" <Michiel.Salter s@logicacmg.com > writes:
            [color=blue]
            > Imre Palik schreef:
            >[color=green]
            > > Karl Heinz Buchegger <kbuchegg@gasca d.at> writes:
            > >[color=darkred]
            > > > ...
            > > > Another method would be to use an inline function. This is somewhat similar
            > > > in that the compiler (not the preprocessor) is able to replace the actual
            > > > call of the function with the function body:[/color]
            > >
            > > Except that there are no in-out register arguments for C++ functions, but
            > > they are possible with macros. Not that it matters too much in the most
            > > cases though.[/color]
            >
            > Wrong. With inline functions, both in and out arguments can be passed
            > in registers.[/color]

            I had something like this in mind

            #define FOOBAR(a, b) a++, b++

            vs.

            inline void foobar(int &a, int &b) {a++; b++}

            I know that in and out parameters can be pased in registers. But in c++,
            if a function parameter is both in and out than it is a reference. AFAIK
            passing a register parameter as reference tends to make the compiler
            disregard the register hint.

            Best regards

            ImRe

            Comment

            • peter.koch.larsen@gmail.com

              #7
              Re: Macros and Executionspeed


              Imre Palik skrev:
              [color=blue]
              >
              > I had something like this in mind
              >
              > #define FOOBAR(a, b) a++, b++
              >
              > vs.
              >
              > inline void foobar(int &a, int &b) {a++; b++}
              >
              > I know that in and out parameters can be pased in registers. But in c++,
              > if a function parameter is both in and out than it is a reference. AFAIK
              > passing a register parameter as reference tends to make the compiler
              > disregard the register hint.
              >[/color]
              What gave you that idea? What compiler will create not optimal code for
              your foobar above?

              /Peter
              [color=blue]
              > Best regards
              >
              > ImRe[/color]

              Comment

              • msalters

                #8
                Re: Macros and Executionspeed


                Imre Palik wrote:
                [color=blue]
                > "msalters" <Michiel.Salter s@logicacmg.com > writes:
                >[color=green]
                > > Imre Palik wrote:
                > >[/color][/color]
                .....[color=blue][color=green][color=darkred]
                > > > Except that there are no in-out register arguments for C++ functions, but
                > > > they are possible with macros. Not that it matters too much in the most
                > > > cases though.[/color]
                > >
                > > Wrong. With inline functions, both in and out arguments can be passed
                > > in registers.[/color]
                >
                > I had something like this in mind
                >
                > #define FOOBAR(a, b) a++, b++
                >
                > vs.
                >
                > inline void foobar(int &a, int &b) {a++; b++}
                >
                > I know that in and out parameters can be pased in registers. But in c++,
                > if a function parameter is both in and out than it is a reference. AFAIK
                > passing a register parameter as reference tends to make the compiler
                > disregard the register hint.[/color]

                AFAIK (and that is confirmed by multiple sources) any modern compiler
                will disregard the "register" keyword anyway. They'll look at the
                actual variables instead. Futrhermore, "passing a parameter" doesn't
                make a lot of sense when inlining. Modern compilers simply alias
                reference arguments. I.e. when inlinig foobar() above, a is made an
                alias of the corresponding argument of the outer scope. This can
                actually happen before the register allocation stage.

                HTH,
                Michiel Salters

                Comment

                • Dave Rahardja

                  #9
                  Re: Macros and Executionspeed

                  On 21 Sep 2005 05:41:11 -0700, "msalters" <Michiel.Salter s@logicacmg.com >
                  wrote:
                  [color=blue][color=green]
                  >> I know that in and out parameters can be pased in registers. But in c++,
                  >> if a function parameter is both in and out than it is a reference. AFAIK
                  >> passing a register parameter as reference tends to make the compiler
                  >> disregard the register hint.[/color]
                  >
                  >AFAIK (and that is confirmed by multiple sources) any modern compiler
                  >will disregard the "register" keyword anyway.[/color]

                  Speak for yourself ;-) I use a modern embedded C++ compiler that respects the
                  "register" keyword to the point of de-optimizing the function call (i.e.
                  stuffing parameters on the stack instead of using registers) if you declare
                  enough variables with the register keyword.

                  Comment

                  Working...