Loading...
Pages: [1] 2 3 4   Go Down
Author Topic: Function() vs Speed  (Read 862 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Edison Member
*
Karma: 23
Posts: 1386
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Don't Be Upset By The Results You Didn't Get With The Work You Didn't Do

New Jersey
Offline Offline
Edison Member
*
Karma: 24
Posts: 2354
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Edison Member
*
Karma: 23
Posts: 1386
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Critical speed issues?  It may add up to enough.
"Inline Directive" - edify me.
Logged

Don't Be Upset By The Results You Didn't Get With The Work You Didn't Do

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 316
Posts: 35593
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
"Inline Directive" - edify me.
That's google's job.
Logged

Offline Offline
Edison Member
*
Karma: 23
Posts: 1386
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Don't Be Upset By The Results You Didn't Get With The Work You Didn't Do

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3117
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Edison Member
*
Karma: 23
Posts: 1386
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Don't Be Upset By The Results You Didn't Get With The Work You Didn't Do

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 316
Posts: 35593
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Edison Member
*
Karma: 23
Posts: 1386
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.'
Logged

Don't Be Upset By The Results You Didn't Get With The Work You Didn't Do

West Des Moines, Iowa USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 429
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

There's always a better way!

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 43
Posts: 2518
"We're a proud service of the Lost Electricity Reclamation Agency"
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Get the infamous "One Million Ohms" board at tINDIE.com: http://tinyurl.com/BuyMohms

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3117
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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++
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 120
Posts: 10201
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 120
Posts: 10201
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but now I figure it may be a hindrance

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

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

Pages: [1] 2 3 4   Go Up
Print
 
Jump to: