Offline
Edison Member
Karma: 23
Posts: 1386
Now, More Than Ever
|
 |
« on: March 10, 2012, 09:23:43 am » |
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
Edison Member
Karma: 24
Posts: 2354
|
 |
« Reply #1 on: March 10, 2012, 09:29:34 am » |
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
Edison Member
Karma: 23
Posts: 1386
Now, More Than Ever
|
 |
« Reply #2 on: March 10, 2012, 09:55:17 am » |
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
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #3 on: March 10, 2012, 10:08:12 am » |
"Inline Directive" - edify me. That's google's job.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 23
Posts: 1386
Now, More Than Ever
|
 |
« Reply #4 on: March 10, 2012, 10:25:51 am » |
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
Faraday Member
Karma: 19
Posts: 3117
|
 |
« Reply #5 on: March 10, 2012, 10:30:32 am » |
Whereas googling for 'c inline directive' produced about 1700000 results, many of which looked quite useful.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 23
Posts: 1386
Now, More Than Ever
|
 |
« Reply #6 on: March 10, 2012, 10:39:55 am » |
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
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #7 on: March 10, 2012, 10:43:23 am » |
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
Edison Member
Karma: 23
Posts: 1386
Now, More Than Ever
|
 |
« Reply #8 on: March 10, 2012, 11:03:54 am » |
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
Sr. Member
Karma: 2
Posts: 429
|
 |
« Reply #9 on: March 10, 2012, 11:18:45 am » |
From ISO/IEC 9899 (the C language standard) 6.7.4.5: 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
Faraday Member
Karma: 43
Posts: 2518
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #10 on: March 10, 2012, 11:20:14 am » |
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
|
|
|
|
|
Gosport, UK
Offline
Faraday Member
Karma: 19
Posts: 3117
|
 |
« Reply #11 on: March 10, 2012, 11:29:56 am » |
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
Edison Member
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
|
 |
« Reply #12 on: March 10, 2012, 02:54:25 pm » |
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
Shannon Member
Karma: 120
Posts: 10201
|
 |
« Reply #13 on: March 10, 2012, 05:48:11 pm » |
Doesn't the Arduino IDE optimise for size over speed? Yes. 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
Shannon Member
Karma: 120
Posts: 10201
|
 |
« Reply #14 on: March 10, 2012, 05:50:48 pm » |
but now I figure it may be a hindrance call --> 4 cycles ret ---> 4 cycles (4 + 4) * 5 / 16 = 2.5 microseconds.
|
|
|
|
|
Logged
|
|
|
|
|
|