Sign in to follow this  
jake_Ghost

Question about inline function

Recommended Posts

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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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; }
...
};

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this