< Previous | Contents | Next >
To mark a function for inlining, simply put inline before the function definition. That’s what I do when I define the following function:
inline int radiation(int health)
Note that you don’t use inline in the function declaration:
int radiation(int health);
By flagging the function with inline, you ask the compiler to copy the function directly into the calling code. This saves the overhead of making the function call. That is, program control doesn’t have to jump to another part of your code. For small functions, this can result in a performance boost.
However, inlining is not a silver bullet for performance. In fact, indiscriminate inlining can lead to worse performance because inlining a function creates extra copies of it, which can dramatically increase memory consumption.
Hin t
When you inline a function, you really make a request to the compiler, which has the ultimate decision on whether to inline the function. If your compiler thinks that inlining won’t boost performance, it won’t inline the function.