Thread: Function call: how does it cost?

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    50

    Function call: how does it cost?

    Hi guys,
    I have a bigger function that calls, in mean 1000-2000 times, a smaller function. I "discover" that calling the smaller function make the code very slow. Is there any hint to fix it? I read about inline functions, but they did not help me...
    Thank you!
    ai confini della conoscenza

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Declaring the function inline is your best bet, unless you want to turn the smaller function into a macro. Besides declaring the function as inline, the definition of the function should also be available to the caller, i.e., you may need to place it in the header, contrary to normal separation of forward declarations of functions in header files and their definitions in source files.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    50
    awesome! Now the code takes half of the time. I did not put the inline function in the header, but after your advise I did it and it works! Thank you!
    ai confini della conoscenza

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  3. Loading files with a function call
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 04-10-2007, 07:19 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM