inline functions

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

    #1

    inline functions

    When and where should I use inline functions?

    I know any member functions inside classes are automatically inline
    function, how about the functions outside the classes?

    The inline functions is said to avoid the overhead of using the
    ordinary functions. So, do I need to use inline functions as more as
    possible?

  • amparikh@gmail.com

    #2
    Re: inline functions


    asdf wrote:
    When and where should I use inline functions?
    Whenever it is possible, you might use inline functions. Having said
    that you have ato remember a few things.

    1>Making a function inline doesnt always guarentee you a better
    performance. for e.g when there is page thrashing. Read the FAQ at
    www.parashift as it gives some expalaination.
    of course, you can find lots of article if you do a search.

    2>The compiler might choose not to inline it even if you define the
    function to be inline.
    Certain compilers give you an option to force inline which leaves the
    discretion to the programmer. For example Microsoft compilers have the
    __forceinline directive.
    >
    I know any member functions inside classes are automatically inline
    function, how about the functions outside the classes?
    If you declare functions defined outside the class as inline, they
    "could" be inlined.
    >
    The inline functions is said to avoid the overhead of using the
    ordinary functions. So, do I need to use inline functions as more as
    possible?
    Yes and No. Go back to the first 2 points I made above and read the FAQ.

    Comment

    • Torsten Mueller

      #3
      Re: inline functions

      "asdf" <likemursili@gm ail.comschrieb:
      When and where should I use inline functions?
      Forget "inline".

      T.M.

      Comment

      • asdf

        #4
        Re: inline functions

        why?

        Torsten Mueller wrote:
        "asdf" <likemursili@gm ail.comschrieb:
        >
        When and where should I use inline functions?
        >
        Forget "inline".
        >
        T.M.

        Comment

        • asdf

          #5
          Re: inline functions

          thanks for your help.

          amparikh@gmail. com wrote:
          asdf wrote:
          When and where should I use inline functions?
          >
          Whenever it is possible, you might use inline functions. Having said
          that you have ato remember a few things.
          >
          1>Making a function inline doesnt always guarentee you a better
          performance. for e.g when there is page thrashing. Read the FAQ at
          www.parashift as it gives some expalaination.
          of course, you can find lots of article if you do a search.
          >
          2>The compiler might choose not to inline it even if you define the
          function to be inline.
          Certain compilers give you an option to force inline which leaves the
          discretion to the programmer. For example Microsoft compilers have the
          __forceinline directive.
          >

          I know any member functions inside classes are automatically inline
          function, how about the functions outside the classes?
          >
          If you declare functions defined outside the class as inline, they
          "could" be inlined.
          >

          The inline functions is said to avoid the overhead of using the
          ordinary functions. So, do I need to use inline functions as more as
          possible?
          >
          Yes and No. Go back to the first 2 points I made above and read the FAQ.

          Comment

          • David W

            #6
            Re: inline functions

            "Torsten Mueller" <dev-null@shared-files.dewrote in message news:u1wpv5jhx. fsf@shared-files.de...
            "asdf" <likemursili@gm ail.comschrieb:
            >
            When and where should I use inline functions?
            >
            Forget "inline".
            They can make a big difference, though you might well be able to forget them if the compiler is
            smart enough.

            DW


            Comment

            • Martin Steen

              #7
              Re: inline functions

              asdf wrote:
              When and where should I use inline functions?
              >
              I know any member functions inside classes are automatically inline
              function, how about the functions outside the classes?
              >
              The inline functions is said to avoid the overhead of using the
              ordinary functions. So, do I need to use inline functions as more as
              possible?
              >
              Inline functions make your code faster but larger.

              Faster, because there is no overhead for calling the function.
              Larger, because everywhere you call the function the whole function-code
              is included.

              It's your own decision where to use inline. I only use it on small
              functions or operators - and as a replacement for C-style macros.

              -Martin

              Comment

              • Torsten Mueller

                #8
                Re: inline functions

                "asdf" <likemursili@gm ail.comschrieb:
                When and where should I use inline functions?
                Forget "inline".
                >
                why?
                Beginners learn very often something about inline functions and then
                they are sure inline functions are the key to success. All my students
                did indeed know this and used "inline" a lot.

                Indeed inline functions make a program faster. But on a modern GHz CPU
                in event driven GUI applications on multitasking operating systems you
                will hardly see a real difference. In most cases your application has
                a lot of much more efficient ways to become faster, especially the
                improvement of your design and the use of better algorithms. I guess
                using "inline" normally leads to less than a promille more speed
                (except very special and seldom cases).

                Oftenly the compiler even doesn't generate the your inline function as
                inline code because it is virtual or too large or it contains a loop
                or something. Other compilers generate inline code even if you don't
                qualify your function with "inline". I find the generation of inline
                code is indeed a compiler optimization. And the compiler itself should
                be responsible for that.

                The keyword "register" is another relict from former times of early
                compilers without good optimizations. Just don't use it anymore except
                you really know that this works in a very special case on a specific
                processor.

                T.M.

                Comment

                • Martin Steen

                  #9
                  Re: inline functions

                  Torsten Mueller wrote:
                  Beginners learn very often something about inline functions and then
                  they are sure inline functions are the key to success.
                  LOL
                  Indeed inline functions make a program faster. But on a modern GHz CPU
                  in event driven GUI applications on multitasking operating systems you
                  will hardly see a real difference.
                  That depends on your application. I sometimes write file-converters that
                  work on files that are more that 100MB large. And you can believe me
                  that there is a difference if I use inline functions or not.
                  But I use them only for small functions and operators.

                  In most cases your application has
                  a lot of much more efficient ways to become faster, especially the
                  improvement of your design and the use of better algorithms. I guess
                  using "inline" normally leads to less than a promille more speed
                  (except very special and seldom cases).
                  For me, these "special cases" are my daily work ;)
                  The keyword "register" is another relict from former times of early
                  compilers without good optimizations.
                  That's true. I suppose most modern compilers ignore the register keyword
                  at all.

                  Best regards, Martin

                  Comment

                  • Greg Comeau

                    #10
                    Re: inline functions

                    In article <1159506646.438 171.266350@k70g 2000cwa.googleg roups.com>,
                    asdf <likemursili@gm ail.comwrote:
                    >When and where should I use inline functions?
                    >
                    >I know any member functions inside classes are automatically inline
                    >function, how about the functions outside the classes?
                    >
                    >The inline functions is said to avoid the overhead of using the
                    >ordinary functions. So, do I need to use inline functions as more as
                    >possible?
                    I just added this as food for thought:


                    I'll spit shine it over the next few day as I've probably
                    misstated something and left out a bunch of stuff.
                    --
                    Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
                    Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
                    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
                    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

                    Comment

                    Working...