When should I really use an inline function? I've read contradicting* articles on it. Should it be small and not called alot? Or small and called alot? Would assigning 7 values to an array of 600 elements be a good candidate?
When should I really use an inline function? I've read contradicting* articles on it. Should it be small and not called alot? Or small and called alot? Would assigning 7 values to an array of 600 elements be a good candidate?
AOL: GrNxxDaY
IDE: Dev-C++ Beta 5 (v4.9.4.1)
Project: Eye of Sahjz (text-RPG)
If you think I may need help, please IM me.
A few general rules ....
Code inline if .......
1. The function has a small number of actions.
2. The function has no loop.
3. The function has no if statements.
4. The function has no switch constructs.
5. The function does not call an external function.
So, generally if your function fails to meet one of these conditions, prototype it within the class and code it outside of the class.
Thankyou, that clears things up a bit.
But I"m still wondering about how often an inline function should be called? If it has only one action but is called 600 times, would it be a good idea to inline it? What are the consequences/benefits?
AOL: GrNxxDaY
IDE: Dev-C++ Beta 5 (v4.9.4.1)
Project: Eye of Sahjz (text-RPG)
If you think I may need help, please IM me.
Generally if you have a function like
You should inline on the spot if this is in a bottleneck of the program, otherwise you would probably do just as without inlining. I think if it's small and not called alot then it's not taking up alot of cpu time anyways and so you shouldn't inline it?Code:class A { public: int do_s() { s++; return s; } private: int s; };
Probably not. I think most of the time would be inWould assigning 7 values to an array of 600 elements be a good candidate?
accessing the memory of the array. For cases your unsure
you should profile and read the assembly listing.
The inline keyword is only a suggestion to the compiler. This means simple functions will probably be compiled as inline function during compiler optimisation anyway.
When you declare a function inline, the compiler doesn't call the function, it places the functions code in your program so it will seem as if there is no function.
Eg.So then compiler would replace the function call with number + 1, so the statement would be something like this,Code:#include <iostream.h> inline int function(int num){ return num + 1; } int main() { int number; number = function(1); cout << number << endl; return 0; }
number = number + 1;
WOOO I GET IT! thanks doggy, that helped
AOL: GrNxxDaY
IDE: Dev-C++ Beta 5 (v4.9.4.1)
Project: Eye of Sahjz (text-RPG)
If you think I may need help, please IM me.