Thread: function overhead?

  1. #1
    Registered User Tox|k's Avatar
    Join Date
    May 2008
    Posts
    6

    function overhead?

    Hey all,

    I'm just wondering about the amount of overhead a function incurs?

    More specifically, I'm writing some 3D related code that makes use of several functions which perform very basic operations on vectors (3D vectors, not the C++ templates) which are basically just an array of 3 floats.
    One such function would be adding two vectors, ie.
    Code:
    void addVect(float *dst, float *v1, float *v2) {
       dst[0] = v1[0] + v2[0];
       dst[1] = v1[1] + v2[1];
       dst[2] = v1[2] + v2[2];
    }
    Since these operations will be carried out a great deal, I'm wondering if anyone has any insights on the overhead incurred by using a function, as opposed to a macro for this.

    I plan on doing some benchmarks to find out anyway, but I wouldn't mind knowing some more about what's going on, and why.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you have a decent compiler, the overhead might be "nothing", as the compiler may well optimize out the entire function call and just inline the actual calculation.

    If you haven't got a decent compiler, then the suggestion would be to get one.

    gcc 3.x and Visual Studio 2005/2008 are both great compilers.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User Tox|k's Avatar
    Join Date
    May 2008
    Posts
    6
    Thanks matsp,

    I did some benchmarking, and, in VS2005 there was no overhead, so, I'm guessing it just inlined the code.

    I also tried it with Borland 5 in which case there was significant overhead. The functionalized algorithms took 4-7 times longer than the macros.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Note in C99 or C++ you can force it to be inline -- with the inline keyword.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by zacs7 View Post
    Note in C99 or C++ you can force it to be inline -- with the inline keyword.
    No, you can't FORCE an inline with the inline keyword. It is just a suggestion to the compiler that it should inline the function - older compilers are more likely to take note of this, modern (e.g. gcc 3+) will inline functions by itself, without any such requests, if it thinks it's the right thing to do.

    Some compilers (e.g. MS) has a "forceinline" keyword that will put more stress on the "I want this inlined", but it's still possible that the compiler decides for some reason that it's not going to inline something.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    That's what I meant

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I'm wondering if anyone has any insights on the overhead incurred by using a function,
    Compile the function, and look at the assembler. How many instructions are actually implementing your code, and how many are just there to make it easy to call the function.

    Typically, I count (in approximate cost terms)
    - each parameter as being the same as a simple assignment.
    - the call itself as two simple assignments (fetch the address is 1, the broken pipeline is the other).
    - the function prologue and epilogue each as a simple assignment (create/destroy stack frame)
    - the return and clean up the stack again as 2 simple assignments.

    Your example on an X86 would be pretty horrid to call as a function. Although you're indexing arrays, the x86 has indexing instructions which mean x[const] is efficiently encoded.

    On the other hand, the RISC machine I was using some time ago passed the first 4 params in registers, and did away with the whole stack frame bit if the function was a leaf function (one which doesn't call anything else).

    > I also tried it with Borland 5 in which case there was significant overhead.
    Debug or release?
    Debug is "do what I ask", Release is "do what I mean" kinda thing.

    Also, prior to C99, C lacked the 'inline' keyword, but that doesn't mean to say that all compilers ignore it. Even without it, look for compiler options which might indicate 'inline short functions'.

    Making the function 'static' can also help, to give the compiler the hint that nothing outside the module can call it.
    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.

  8. #8
    Registered User Tox|k's Avatar
    Join Date
    May 2008
    Posts
    6
    Thanks for all the info.

    Anyone know of an easy way to view the ASM that's generated for a function? I haven't used ASM much, so I don't know the best tools and stuff for it. I don't suppose there's just an easy way to do this is VS2k5?

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Tox|k View Post
    Thanks for all the info.

    Anyone know of an easy way to view the ASM that's generated for a function? I haven't used ASM much, so I don't know the best tools and stuff for it. I don't suppose there's just an easy way to do this is VS2k5?
    You can pop an assembly window from the debugger...

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Or go to your project/file properties in the project view and set "listing" to "Assembler + Source" (I think that's /FAs if you use command line code generation).

    Welcome to the world of "Studying compiler output".
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    With VS200X, all you need to do is press Alt+8 to see the assembly while debugging.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  5. dynamic function execution
    By Sargnagel in forum C Programming
    Replies: 7
    Last Post: 05-07-2003, 05:28 AM