Hi,

I'm optimising my code, mainly for size. Knowing how much better
code SDCC creates for global variables I made an interesting comparison
which I thought I share here, see code below.

Interestingly the code (bar4) that four times expands the whole
'get position'  calculation is smaller (585 < 622) than the more
conventionally coded subroutine (bar5) based version.

And, maybe not surprisingly, it is about four times faster.
(The execution times are of course estimate based on code
size and the execution path assumption).

Without testing conventional wisdom would have led me to
believe that the macro version would be much bigger than
the subroutine based one.

br Kusti






stepper_state_t g_stepper_states[4];

#define GET_POSITION(i) \
  (g_stepper_states[i].last_dir)?\
  ( g_stepper_states[i].position + (g_stepper_states[i].last_steps -
g_stepper_states[i].steps))\
 :\
  ( g_stepper_states[i].position - (g_stepper_states[i].last_steps -
g_stepper_states[i].steps))\


int32_t get_position(uint8_t i) { // bytes 434 cycles ~217
 return  (g_stepper_states[i].last_dir)?
  ( g_stepper_states[i].position + (g_stepper_states[i].last_steps -
g_stepper_states[i].steps))
 :
  ( g_stepper_states[i].position - (g_stepper_states[i].last_steps -
g_stepper_states[i].steps));
}

int32_t pos=0;

void bar4() { // bytes 582 cycles ~291
 pos=GET_POSITION(0);
 pos=GET_POSITION(1);
 pos=GET_POSITION(2);
 pos=GET_POSITION(3);
}

void bar5() { // bytes 188 cycles 188
 pos=get_position(0);
 pos=get_position(1);
 pos=get_position(2);
 pos=get_position(3);
 }

 // bar 4 = bytes 582               cycles  291
 // bar 5 = bytes 622 = 188 + 434   cycles ~1056 = 188 + 4 * 217~


This e-mail may contain confidential or privileged information. If you are not 
the intended recipient (or have received this e-mail in error) please notify 
the sender immediately and destroy this e-mail. Any unauthorized copying, 
disclosure or distribution of the material in this e-mail is strictly 
forbidden. We will not be liable for direct, indirect, special or consequential 
damages arising from alteration of the contents of this message by a third 
party or as a result of any virus being passed on or as of transmission of this 
e-mail in general.

------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=190641631&iu=/4140/ostg.clktrk
_______________________________________________
Sdcc-user mailing list
Sdcc-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sdcc-user

Reply via email to