Function calls

Go To Last Post
16 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I want to know what is expensive,
making two functions or having a single parameter and if loops. see below

void motor_left_dir(uint8_t direction){
...
}
void motor_right_dir(uint8_t direction){
...
}

Or

void motor_dir(uint8_t motor, uint8_t direction)
{
	if (motor==M_LEFT){ 
		...
	}
	else { // motor==M_RIGHT
		...
	}
}

Thanks
K

Last Edited: Tue. May 7, 2019 - 12:52 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Define "expensive" - run time speed, executable size, C-code size, C-code maintainability, ... ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Define expensive? Execution time, code size, ram usage , defects or source code size?
I'll assume execution time, however code size and ram usage can be critical in AVR applications.
Your question is difficult to answer - optimising compilers can pull various tricks that could favour either case. To get an answer you would have to time each method. Write the code so that it is understandable and actually works first. You can then measure how long it takes to execute. Armed with this information, you can then make an informed decision on whether you need to optimise or not.

Martin - you sound like an engineer! Qualify and quantify!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well I do not have exact need for this optimization, I ask this out of curiosity,

I use avrgcc, call to these functions will be from my state machine and I believe if a function is accessed again and again there is a cache that can be used to reach it, in contrast if there are two different function they are alternatively used then cache isn'tused ... Making it more expensive in terms of execution speeds with lessor overheads in compare with former .

Am I correct there, with respect to avrgcc.

@martin I will try out your suggestion too.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No cache. It (the function) sits there in ROM. There is same cost (per access) accessing it once or one million times.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"I believe if a function is accessed again and again there is a cache that can be used to reach "
well, are avr such sophisticated?
and how could functions such 'delay_ms ' be used reliably, if there were such a cache?
There is a little overhead , putting an extra variable on the stack, and some tests...
Do you use a makefile to invoke gcc? if you do, maybe there is a rule to generate a *.lss (assembly listing, with original c as comments). Else, avr-gcc -S can generate assembly (but difficult to read)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
Martin - you sound like an engineer! Qualify and quantify!
Doh. The cat is out of the bag. Mea culpa :D

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There is always a speed vs space tradeoff. My rule for collecting common code into a subroutine is: do I use it more than twice? Example: I have a spaces(char n) that calls space(void) that calls putc(' '); If one finds oneself formatting stuff on the screen, calling spaces(5) in 2 or 3 places wins over calling puts(" "); in flash bytes. Putting the 2 or 3 lines of a small function in a macro (manual inlining) is faster by 5 cycles needed for the call and return. Not passing params saves space for the instructions that create the stack frame and remove it, and also save the cycles needed to run those instruction. Etc. You need to look at the assembler output by the compiler.

Imagecraft compiler user

Last Edited: Tue. Mar 19, 2013 - 11:47 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Me thinks the OP believes that an AVR works like big-silicon CPUs with pipelines and local caches and all that stuff. AVRs do have a very small pipeline that helps to achieve the one instruction per clock speed. But, none of the rest of that stuff.

For all practical purposes, each instruction is read directly from ROM and is immediately executed.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

bobgardner wrote:
There is always a speed vs space tradeoff. My rule for collecting common code into a subroutine is: do I use it more than twice? Example: I have a spaces(char n) that calls space(void) that calls putc(' '); If one finds oneself formatting stuff on the screen, calling spaces(5) in 2 or 3 places wins over calling puts(" "); in flash bytes. Putting the 2 or 3 lines of a small function in a macro (manual inlining) is faster by 5 cycles needed for the call and return. Not passing params saves space for the instructions that create the stack frame and remove it, and also save the cycles needed to run those instruction. Etc. You need to look at the assembler output by the compiler.

These functions are called 100s of times so putting them in function is wise ... you suggest having function call with two parameter is less instructions to maintain the stack.and it doesn't matter if there are two functions.

Oh btw when I see I don't understand asm a lot ... I use avr studio 6.1b

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
and how could functions such 'delay_ms ' be used reliably, if there were such a cache?
Well, _delay_ms is really a macro that produces inline assembly, so that is probably not a good example.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In the OP the second code is doing a test that's not in the first so the initial appearance is that it'll be larger and slower code but, then again, with the two separate functions there's probably code making a decision as to which to call at the point of invocation so the chances are it's the same number of flash bytes and CPU cycles overall.

As others have said the AVR is a very raw simple processor it doesn't have advanced features such as pipelines and instruction / data caches as found on more advanced processors like ARM or Intel.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@Koshi : did not figure out it was a macro (but if it were not, it would be able to accept variables as arguments!!.... -> I should have..)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, the wrapper might actually be a static inline function and the inner working a macro, so that when called with a constant the optimizer can remove the function part and keep the macro part.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I use avrgcc, call to these functions will be from my state machine and I believe if a function is accessed again and again there is a cache that can be used to reach it, in contrast if there are two different function they are alternatively used then cache isn'tused

From where does this belief stem?

Unlike e.g. Intel/AMD/... processors in modern PCs AVR have no Ln (or In, Dn) caches between the flash/RAM memories and the CPU. Each time a fetch/read/write needs to be done it is really done from/to the actual memory.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
Oh btw when I see I don't understand asm a lot ... I use avr studio 6.1b

Yes, but avrstudio can generate *.lss (asm with the original c lines as comments) You can try to understand them (on a small program :this is the way, from Clawson's posts, I began reading atmel's assembly -forgot assembly 30yrs ago) or post (parts of) it, for explanations...