Inline Functions

This is a discussion on Inline Functions within the C++ Programming forums, part of the General Programming Boards category; Would these two functions be good candidates for Inlining? Code: void SetCursor(int x, int y) { Position.X=x; Position.Y=y; SetConsoleCursorPosition(hOut, Position); ...

  1. #1
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96

    Inline Functions

    Would these two functions be good candidates for Inlining?

    Code:
    void SetCursor(int x, int y)
    {
        Position.X=x;
        Position.Y=y;
        SetConsoleCursorPosition(hOut, Position);
        return;
    }
    
    
    string ReadCursor(int x, int y, int z)
    {
        Position.X = x;
        Position.Y = y;
        char tempchar[25] = "\0";   
        string tempstring;
        ReadConsoleOutputCharacter(hOut,
                                   tempchar,
                                   z,
                                   Position,
                                   &NumRead);
        tempstring.assign(tempchar, 25);
        return tempstring;
    }
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    1
    Quote Originally Posted by rwmarsh
    Code:
    void SetCursor(int x, int y)
    {
        Position.X=x;
        Position.Y=y;
        SetConsoleCursorPosition(hOut, Position);
        return;
    }
    Why do you use an return at the end of the function?

  3. #3
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96
    I always thought the syntax required a return at the end of function definitions.
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Your compiler likely does a better job than you at optimisation. Usually the only functions that I inline are 1-4 liners, but I'm sure the compiler does it anyway - inline is only a hint, like register.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> tempstring.assign(tempchar, 25);
    You should use tempstring = tempchar; unless you really want the string to have exactly 25 characters. If tempchar is a string with less than 24 characters, then after the terminating null there will be garbage characters. The assign function will copy the terminating null and those garbage characters into tempstring and tempstring's length will be 25 no matter what. If you use the assignment operator, tempstring will only receive the string contents of tempchar. Output tempstring.length() to verify if the string is getting the correct data.

    >> Would these two functions be good candidates for Inlining?
    Probably not, unless you've profiled your program and found that those functions are called a ton of times and the overhead is slowing your program down.

    >> I always thought the syntax required a return at the end of function definitions.
    On void functions that obviously don't return anything it is not necessary.
    Last edited by Daved; 03-16-2006 at 11:48 AM.

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Quote Originally Posted by rwmarsh
    I always thought the syntax required a return at the end of function definitions.
    Not a requirement, but it doesn't hurt either. If you like it, use it.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,531
    Any function is a candidate for you to attempt to inline if you care about the performance when using it a lot more than you care about with other functions.

    As to whether you will get a benefit from inlining, that depends on the compiler and whether it takes the hint when you've inlined the funcion. Some compilers refuse to inline any functions that do more than get or set a variable (i.e. setters and getters). Some compilers refuse to inline functions that include any looping constructs.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    If your function calls are costing you precious milliseconds, the overall overhead runs into whole seconds, they are mostly wrapper/nearly-do-nothing functions, they do not use up much memory, and (most importantly) you feel they should be inlined, then go ahead.

    Short answer, based on what you've posted so far: go ahead.

    Note: some compilers will refuse to inline functions that are too big (think 10s of lines and involving some graphics card driver manipulation), no matter how much you want them to.

  9. #9
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    I would say You have to play with it to see what is best

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    I remember reading this on one of the C++ usenet groups a while ago
    The first rule of optimisation : Don't!


    (Of course - this is in reference to the fact that one should always iron out all bugs in their code and test vigourously before attempting any sort of optimisation)

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,531
    The basic intent of the first rule of optimisation is to avoid the phenomenon of premature optimisation --- people wasting lots of time trying to optimise something when there is no need to.

    The time to do optimisation is when you have developed a program and have it running correctly. At that point you can identify any performance shortfalls, and identify the main contributors to any poor performance (eg using a profiler to identify what code the program is executing when it runs too slowly). Once you've identified the main contributors (often described as "hot spots") you optimise them.

    Unfortunately, a beginners trap is start optimising too soon, and waste a lot of time hand-crafting code to optimise everything. That is a waste of time as it usually means optimising things that don't need to be optimised, and often also misses the real hotspots anyway (hotspots often turn out to be very small areas of code which, when optimised, give significant returns on effort).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inline Definitions and Declarations?
    By legit in forum C++ Programming
    Replies: 1
    Last Post: 06-15-2009, 02:59 PM
  2. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-26-2009, 12:21 AM
  3. When to inline your *tors
    By Angus in forum C++ Programming
    Replies: 43
    Last Post: 10-29-2008, 04:38 PM
  4. conditional breakpoints and inline functions
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 08-10-2006, 09:30 PM
  5. inline friend functions.
    By sean in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2002, 12:37 PM

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