View Full Version : Performance: inline functions
12-11-2000, 02:12 AM
Are inline functions in C++ faster than normal functions? Have I to write "inline" before the function indentifier when declaring it?
12-11-2000, 03:03 AM
Theorectically, inline functions are faster than a standard function call as it avoids the overhead of a series of stack push/pops relating to the current execution location in memory, the area of memory occupied by the function and whatever arguments need to be passed.
However, it's important to remember that an implementation reserves the right to reject a 'request' for inlining a function, normally if it is either too large or called too frequently.
Signaling to the compiler that you would like a function to be inline is indeed as simple as prefixing the inline qualifier, followed by a space, to the return type in the prototype of the function, i.e the declaration.
Note: Although the execution of the program may be slightly faster, there is a chance the executable will be larger.
Also, it's important to first identify the bottleneck of your application when optimizing code. There is little use trying to enhance the application stage if your program is bound by rasterization as an example...
12-11-2000, 05:35 AM
Inlines are perfect for user defined types. Like Point3D, Vector3D, Matrix3D - classes, where you define operators and function on them. Those functions are called very often and can give a speedup in 3D calculations.
12-11-2000, 08:17 AM
If you're using VC++ 6 (or I believe 5), there's an optimization option to inline all suitable functions, not just the ones explicitly marked. Of course, the compiler will still reject some inline commands and may not inline functions you'd like, so I'd suggest both marking them inline and using the option.
12-11-2000, 09:53 AM
Also a few other things to be aware of with inlining.
First, inlining can actually slow an app down in some circumstance. Sometimes non-inlined code will fit in cache, but once it is inlined it becomes to big to fit in the cache and can actually be slower. I don't know of any way to measure this or to predict when it might happen (especially since it is processor specific), but if the code appears to slow down after inlining, this could be the reason.
Second, I don't know for certain, but I recall someone once told me that if you have a function declared in a .h file and implemented in a .cpp file, it will not get inlined even if you declare it so (ie: you have to declare AND implement it in the .h file). I dont know if this is true, and if so, I dont know if it applies to VC++ or all compilers. Does anyone remember hearing anything like this?
12-11-2000, 09:59 AM
Yes, LordKronos, I know it to be true in VC++ 6, because if you declare a prototype as inline, and it's not in the .h, you'll get unresolved external link errors
12-11-2000, 12:25 PM
Well, are you sure that I have to write "inline" before a function? I have a book that says that to make a inline function I only have to implement it in the .h file, and withhout writting "inline" before the identifyer.
12-12-2000, 01:35 AM
To be more precise:
- 'inline' keyword is just a hint for the compiler. Not an order. NEVER an order, even with all the flags/hints/clue you may put around.
- to actually inline a function, it's code MUST (!) be in the .h file, not in the .cpp,
otherwise it'll be simply linked just as any other function.
(The compiler must know the code to generate when it found the function name.)
This is a C++ need. Not a VC++ issue.
- Some function WILL get inlined even if you don't explicitly specify 'inline'. This happens when the function is small enough AND the code is in the .h (inside or outside the class declaration, no matter).
CGLVector.h file start here
...(very small : 2 lines or so. NOT 200...)
end of .h
'normalize' WILL be inlined, you can bet!
(well, 90% sure)
REMEMBER : if somewhere in your code you use the address of 'normalize', then it CANT be inlined anymore (what would be its address, anyway)
Actually, if you unscramble the ASM code, you may see that sometimes its inlined, and sometimes it's not, in the SAME exe.
It may depends on weather and wind direction... http://www.opengl.org/discussion_boards/ubb/wink.gif You may never know for sure.
12-12-2000, 07:20 AM
One word to optimization with VC++: If you don't really need it, don't use it! I've spent hours of searching for errors in my code until finding it on the assembler level: The compiler produced wrong code! Everything worked fine after disabling optimizations.
Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.