Go Down

Topic: Function() vs Speed (Read 3402 times) previous topic - next topic

Runaway Pancake

In my project I repeatedly call a function, it's 5 instructions.  That helped in development, but now I figure it may be a hindrance.
Say that conserving memory is not a consideration - I will speed up execution overall if I just repeat the block of code in place of the function call, won't I?
Put another way, am I correct in reckoning that there's a certain "overhead" (extra time) that results from a function call?
"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

wildbill

Yes, there is overhead with functions, although do you really have such critical speed issues that it'll make a difference? If so, the inline directive is what you want - best of both worlds.

Runaway Pancake

Critical speed issues?  It may add up to enough.
"Inline Directive" - edify me.
"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

PaulS

Quote
"Inline Directive" - edify me.

That's google's job.

Runaway Pancake

I used the forum search (inline directive) and turned up a dozen or so hits.
Two mentioned using inline directives, one made a half-hearted explanation (in a half-dozen words), neither provided an example.
"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

dxw00d

Whereas googling for 'c inline directive' produced about 1700000 results, many of which looked quite useful.

Runaway Pancake

I guess that I should just have known, somehow, to add a C in my search term.
"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

PaulS

Quote
I guess that I should just have known, somehow, to add a C in my search term.

Or C++. After all, you are looking for how to use "inline" with a specific language.

Runaway Pancake

If the title of my Subject had been, "What's a inline directive?", I could get the sharp elbows.
But since this inline directive prospect came up in the course of conversation, and I kind of thought that's what's supposed to go on around here, having a conversation, I figured maybe I'd ask my good friend to drop the other shoe as it were. 
I don't equate that with asking to be spoon-fed or having the world presented me on a silver platter.  If someone had simply replied along the lines of, "Oh, that's where you... and then..." then I'd probably figure, 'Hmmm, grist for the mill there, mate.  Thanks.'
"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

Morris Dovey

From ISO/IEC 9899 (the C language standard) 6.7.4.5:
Quote
A function declared with an inline function specifier is an inline function. The function specifier may appear more than once; the behavior is the same as if it appeared only once. Making a function an inline function suggests that calls to the function be as fast as possible. The extent to which such suggestions are effective is implementation-defined.


I don't know how the directive is handled in C++ or the Arduino toolset, but if the time savings is essential, then it may be worth writing a macro containing the five instructions (statements?) and invoking the macro where needed.
There's always a better way!

Jack Christensen

There's a decent tutorial at http://www.cplusplus.com/, the inline specifier is described therein. Have not used it myself, but I believe it falls into the category of a "suggestion" to the compiler. The GCC compiler is pretty good at optimizing, I suppose it's possible that it could even decide to inline a function without being told. OTOH, there could be compiler switches that control whether it does or not. It'd be interesting to review the generated assembly language code to see what's really going on. If you happen to do that, we'd be interested in what you find!
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

dxw00d

Doesn't the Arduino IDE optimise for size over speed? If it does, I would think it would ignore the suggestion, unless you overrode the optimisation.

tuxduino

Code: [Select]
avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=100

-Os = optimize for size.

I puzzles me why -g is used together with -Os...

http://linux.die.net/man/1/avr-g++

Coding Badly

Doesn't the Arduino IDE optimise for size over speed?


Yes.

Quote
If it does, I would think it would ignore the suggestion, unless you overrode the optimisation.


Not always.  I have no idea how the algorithm works but I know there are cases with the Arduino options when inline really does result in inlining.  I also know that a static function called once always gets inlined.

There is a GCC compiler directive to force a function to always be inlined.

Coding Badly

Quote
but now I figure it may be a hindrance


call --> 4 cycles
ret ---> 4 cycles

(4 + 4) * 5 / 16 = 2.5 microseconds.

Go Up