Skip to main content
gamedev.net
🔒 Locked

Question about inline function

Started by jake_Ghost 3 replies 450+ views
Original Post
jake_Ghost
jake_Ghost
I have a function that just gets a value in a scoll bar as to where the scroll knob is... this is called alot, so it would be called thousands of times in a short time (couple seconds), would it be wise to inline this or should i just do it normally?

int GetValue() { return cpri_Value_I; }

or

int GetValue()
{
    return cpri_Value_I;
}

Jake
Jan-Lieuwe
Jan-Lieuwe
if the function is very small or if the compiler can optimize it because it knows the input/output parameters beforehand (in case of constants), then it certainly makes sense to inline it.

The best way to find out, however, is to try both while profiling your code :)

Edit: both functions you posted will do the same. You should add 'inline' to one of the functions. Also make sure you put those functions in your header file if it's a global function (or use .inl files).
SiCrane
SiCrane
In general profilers are very good at telling you what code you should inline that you didn't and not so good at telling you what code you shouldn't inline that you did, so I usually recommend not to inline your functions until the profiler says you should.
LonelyStar
LonelyStar
Just a notice:
Maybe you are aware of this, but the two examples you posted are both NOT inlined.
Inlined would be:

inline int GetValue()
{return cpri_Value_I;}

(Having nothing to do with into how many lines you write your code).
Greetings,
Nathan
JohnBolton
JohnBolton
Quote:
Original post by LonelyStar
Just a notice:
Maybe you are aware of this, but the two examples you posted are both NOT inlined.


... unless he left out some important context in the first case:
    class foo    {        ...        int GetValue() { return cpri_Value_I; }        ...    }; 
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.