menu

hjk41的日志

Avatar

Effective C++ 33: Use inlining judiciously

Inlining a function could fasten your program, but only when inlining is judiciouly used.
Inlining can also casue your program to bloat. And the increase of the code means more cache miss when running your program, thus inlining does not necessarily improve the performance.
Sometimes even you have declared a function inline, the compiler still have to generate a function body for it. For example, if you take a pointer to the function, then a function body will be generated so that the pointer can actually point to something.
Moreover, constructors and destructors are bad candidates for inlining. Though you may write no code in constructors and destructors, the compiler have to take a lot of actions in constructors and destructors. When an object is constructed, memory should be allocated, constructor of the base class should be called, and the member variables should be initiated. All this makes the constructor rather big, though you may not see it. And destructors do the opposite thing, destroying member variables and freeing memory. So we better don't declare constructors and destructors inline.

评论已关闭