Inline function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Inline function

It is a good programming practice to maintain the code of inline function as short as possible. It may improve the execution of the code if used wisely otherwise it may behave inversely. justify with solid reason

14th Aug 2018, 11:14 AM
Shaphine
Shaphine - avatar
8 Answers
+ 3
According to the draft (n4296), §7.1.2, p.151 ¹ " [...] The inline specifier indicates to the implementation that inline substitution of the function body at the point of call is to be PREFERRED ² to the usual function call mechanism. An implementation is NOT ³ required to perform this inline substitution at the point of call [...]. " [emphasize is mine] So, in general, inlining a function isn't something that the programmer should be concerned about. Today's compilers are sophisticated enough to take care of such micro-optimizations during the compilation phase; however we can't blame those programmers who used to give a hint to archaic ones in the past. _____ ¹ http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf ² To eliminate the overhead of the function call and possibly increase a tiny performance. ³ There's no obligation for the compiler to obey what inline says.
14th Aug 2018, 1:13 PM
Babak
Babak - avatar
+ 2
Shaphine The answer is don't make inline functions at all yourself. Leave it to the compiler.
14th Aug 2018, 11:36 AM
Vlad Serbu
Vlad Serbu - avatar
+ 2
Shaphine yes you want to keep it short to maintain improved execution time, however the longer it gets the less efficient it is and can get to the point where its less efficient than the overhead from a non inline function.
14th Aug 2018, 11:44 AM
Robert Atkins
Robert Atkins - avatar
+ 1
are you referring to inline definitions of methods within an object header file? or a different inline function?
14th Aug 2018, 11:17 AM
Robert Atkins
Robert Atkins - avatar
+ 1
i am asking to maintain a code of inline function as short as possible because it improves execution time .Is It so or not ?
14th Aug 2018, 11:34 AM
Shaphine
Shaphine - avatar
+ 1
ok thank you very much
14th Aug 2018, 11:57 AM
Shaphine
Shaphine - avatar
0
If a function is big, you don't inline it. Thankfully, most modern C++ compilers inline small functions anyway so you generally shouldn't be using the inline keyword at all.
14th Aug 2018, 11:23 AM
Vlad Serbu
Vlad Serbu - avatar
0
Shaphine let me tell you what happens when a function is created in code... for each function, compiler creates a function pointer... this pointer is associated at all points when you call a function.. this way, function gets executed by linking with function pointer... so, this extra overhead is there for compiler.. once your code is small for a function, you make a request to compiler for making it inline.. if function is inline, compiler does not create a function pointer and entire function code gets replaced from where your function is called... so, you have to choose what is best from function call overhead or load to copy entire function.. now a days, compiler decides what need to be inline and what don't need to be inline... you can just make request to compiler to make it inline...it's compiler which decide finally...
14th Aug 2018, 11:33 AM
Ketan Lalcheta
Ketan Lalcheta - avatar