inline efficiency, or lack thereof

This is a discussion on inline efficiency, or lack thereof within the C++ Programming forums, part of the General Programming Boards category; Can anyone provide me with an example of an inline function that actually hinders or helps? I was just implimenting ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    19

    inline efficiency, or lack thereof

    Can anyone provide me with an example of an inline function that actually hinders or helps? I was just implimenting a function, which I deemed worthy of being inline but I was then thinking, "does this indeed actually help? In fact, when ever could it help?"

    I'm just curious, if anyone has any examples, links and or data I'd much like to see it.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    This article is a little out of date, but it shows amazing things that are only possible through forcing the compiler to inline stuff...
    http://www.flipcode.com/articles/art...ctormath.shtml

    It's a bit extreme though as I imagine you only want a simple example...

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    inlining always helps speed up the program, but it may also make the program larger. Whether it helps or not depends on what you want to do -- do you want the fastest running program or do you want the smallest program size. Normally one-statement functions should be inlined. There is no guarentee that the compiler will actually inline the function even if you use the inline keyword -- compilers only treat that keyword as a suggestion and it is free to do whatever it wishes.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > inlining always helps speed up the program
    That's an overly bold statement.

    But yes, leave the compiler to worry about inlining, and leave the decision until you've had a chance to do some profiling on a real finished and working program with real data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by Ancient Dragon
    inlining always helps speed up the program,
    That's just false, though.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    inlining always helps speed up the program
    Many modern compilers automatically inline functions if you use them a lot (especially if you optimise for speed). So inlining a function might not do anything because it's already inlined! Compilers are free to ignore the inline keyword, too, and I'm sure some do.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Rashakil Fol
    That's just false, though.
    Nothing false about it. When the program does not have to call a function then it makes the program a tad bit faster. So where is the folly in that statement? This is not the same as saying the inline keyword will always produce inline code -- that is false as I also mentioned in my post.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by dwks
    Many modern compilers automatically inline functions if you use them a lot (especially if you optimise for speed). So inlining a function might not do anything because it's already inlined! Compilers are free to ignore the inline keyword, too, and I'm sure some do.
    Well, duua! we are talking about inlining verses not inlining. inlined functions will ALWAYS make the program run faster than functions that are NOT inlined. This has nothing at all to do with whether the compiler does or does not honor the inline keyword.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Except massively unrolled code might no longer fit in the cache.

    A cache miss to code which isn't yet in cache (or worse, not even in memory) is going to be a lot worse than some nice tight code which has a few function calls, but it small enough to remain cache-resident all through its run.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> Many modern compilers automatically inline functions ... Compilers are free to ignore the inline keyword, too, and I'm sure some do.

    That just makes me wonder this - why did they bother making the inline keyword if the compiler chooses whether or not it is to be used? (I'm only being slightly sarcastic) They probably to do with the number of extra-marital kids the time ... right, Mario? Or is that just for size_types if the number of times the function is called exceeds the former number, it's inlined, else it's not.


    Is it faster at building the exe when the functions are inlined, or if they aren't?

  11. #11
    すまん Hikaru's Avatar
    Join Date
    Aug 2006
    Posts
    46
    Quote Originally Posted by twomers
    why did they bother making the inline keyword if the compiler chooses whether or not it is to be used? (I'm only being slightly sarcastic)
    It's probably like the register keyword. It was a good idea at the time but someone realized that the compiler is better at the job than programmers.

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    Inlining puts an optimization choice in the coder hands. The optimization comes from the fact that at runtime it's possible to avoid the extra cost of a function call. The keyword exists because there are tradeoffs. The most common one is an increase in file size. As such, the decision is left to the programmer to optimize or not.

    It's important to understand that the decision to inline or not is not of the compiler. We speak "the compiler refuses to inline", when in fact probably the more correct way would be "the compiler can't inline". Some functions simply cannot be inlined (imagine a complex recursive function), others would lead to massive changes in code that would not respect anymore the original source.

    However, I would do it differently. If I developed a compiler I would code in decisions to inline or not according to my mood. I don't know... like if an extra-marital kid suddenly showed up at my door.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by Ancient Dragon
    Nothing false about it. When the program does not have to call a function then it makes the program a tad bit faster. So where is the folly in that statement? This is not the same as saying the inline keyword will always produce inline code -- that is false as I also mentioned in my post.
    It's not just false because you could totally flush out the cache (but that's a reason), it's false because function calls are not necessarily more expensive. You can take a function that makes 100 function calls to another and move for loops around so that now you're making 10000 function calls, only to have the version with more function calls run faster. You could do this directly in assembly language and see the result. Or you could get the opposite result.

    People get all jyzed up about function calls, but ultimately, it amounts to pushing one or two pointers onto the stack, and accessing the argument list in the child function. All these are operations in the closest cache to the processor, so these are fast. With all the instruction reordering and crazy stuff that processors do these days, the time you spend pushing the stack might have been spent waiting for addition circuits to clear out anyway, and the way instructions are aligned to word boundaries will have interesting effects.

    Of course, inlining is still a Good Thing.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  14. #14
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    How am I the first one to post this in the thread?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  15. #15
    Registered User Osaou's Avatar
    Join Date
    Nov 2004
    Location
    Stockholm, Sweden
    Posts
    69
    Because you beat me to it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. When to inline your *tors
    By Angus in forum C++ Programming
    Replies: 43
    Last Post: 10-29-2008, 03:38 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Inline functions and inheritance
    By hpy_gilmore8 in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2004, 05:46 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  5. Replies: 5
    Last Post: 09-17-2001, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21