You are not logged in.

#26 2012-07-03 13:30:18

Grinch
Member
Registered: 2010-11-07
Posts: 265

Re: Recursion in C, efficiency vs. elegance

saline wrote:

I meant to suggest that there may be an ideal complexity of a function where the cost of the function call is equivalent to the cost of the stack manipulations incurred by inlined code that performs the same function.  Of course, this would be highly dependent on the context, but being able to identify that level of complexity would mean knowing when to break out code into its own function (for readability).  I doubt this is very practical, however.

Still not quite following you (then again I haven't had my coffee yet), but I have a gut feeling we are just talking past eachother and really mean the same thing.

Anyway, as I see it there are two reasons for splitting up code into smaller functions, one is readability, large functions are less readable. By splitting up functions into focused tasks you get a better overview of the code (IMO). Second is when you have functionality you want to be able to call from several places in your code, you could write this code directly into the functions instead of calling it but that would lead to code duplication thus making the code larger.

So why inline then? Inlining allows you to keep the code in a separate function and have it placed in the appropriate code block at compile-time, this avoids call overhead. Now for call overhead to have a performance impact, the code has to be called at high repeated intervals, like in a tight loop. Also if the function you consider inlining is large/complex and takes alot of time to complete (return from) then the call overhead becomes such a small part of the performance impact of that function that it's likely not worth inlining unless you really need to squeeze out every cycle.

So inlining (IMO) relates to small pieces of code which you could write directly into a function but choose to place externally for either readability purposes and/or because you want to inline that code into other functions aswell, and of course don't want to suffer call overhead.

Offline

#27 2012-07-03 17:01:09

saline
Member
Registered: 2010-02-20
Posts: 86

Re: Recursion in C, efficiency vs. elegance

Grinch wrote:

Still not quite following you (then again I haven't had my coffee yet), but I have a gut feeling we are just talking past eachother and really mean the same thing.

I think you're right.

Offline

Board footer

Powered by FluxBB