EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

Function Calls on ARM

Started by holysmoke February 5, 2007
Hi All,

I was wonder what are relative costs of a function call and a
conditional statement? This is because I'm implementing a state
machine as:

static media_callback media_cb [EVENT_MAX][STATE_MAX] =
{
    {dummy, media_hide, media_hide, media_hide },
     ...
};


Should I call the callback like, with dummy function above

media_cb[media_event][media_state](a,b,c,d,e,f);

Or, is introducing a conditional at the point of call the better
solution? (& of course replacing dummy with NULL in media_cb array
above)

if (media_cb[media_event][media_state] != NULL)
 media_cb[media_event][media_state](x,y,z);

Regards

"holysmoke" <sukrit.mehra@gmail.com> wrote in message 
news:1170654110.328688.216910@q2g2000cwa.googlegroups.com...
> Hi All, > > I was wonder what are relative costs of a function call and a > conditional statement?
A function call is usually more expensive on most CPUs - consider that a call implies 2 branches while a conditional statement is less than 1 branch. However calls are easier to predict on CPUs with branch prediction so if the conditional branch mispredicts often, it will be slower.
> Should I call the callback like, with dummy function above > > media_cb[media_event][media_state](a,b,c,d,e,f); > > Or, is introducing a conditional at the point of call the better > solution? (& of course replacing dummy with NULL in media_cb array > above) > > if (media_cb[media_event][media_state] != NULL) > media_cb[media_event][media_state](x,y,z);
Nobody but you can answer that question. It depends on the relative frequency of the dummy functions vs real functions, how much time is spent in the rest of the code, the exact CPU used and the compiler used (& settings). Benchmark your code to get the answer. Wilco
holysmoke <sukrit.mehra@gmail.com> wrote:
> > I was wonder what are relative costs of a function call and a > conditional statement? >
This depends on a lot of things. Calls tend to be more expensive then branches, and both have different instruction cache behaviour as well. If you want to be sure, write some test code and measure. -- :wq ^X^Cy^K^X^C^C^C^C
On 4 Feb 2007 21:41:50 -0800, "holysmoke" <sukrit.mehra@gmail.com>
wrote:

>Should I call the callback like, with dummy function above > >media_cb[media_event][media_state](a,b,c,d,e,f); > >Or, is introducing a conditional at the point of call the better >solution? (& of course replacing dummy with NULL in media_cb array >above) > >if (media_cb[media_event][media_state] != NULL) > media_cb[media_event][media_state](x,y,z);
From the readability I'd go for the dummy function. Using ARM mode does not add a branch since the call could be conditional. What core does the code run on ? Do you have caches ? -- 42Bastian Do not email to bastian42@yahoo.com, it's a spam-only account :-) Use <same-name>@monlynx.de instead !
On Feb 5, 9:39 pm, bastia...@yahoo.com (42Bastian Schick) wrote:
> On 4 Feb 2007 21:41:50 -0800, "holysmoke" <sukrit.me...@gmail.com> > wrote: > > >Should I call the callback like, with dummy function above > > >media_cb[media_event][media_state](a,b,c,d,e,f); > > >Or, is introducing a conditional at the point of call the better > >solution? (& of course replacing dummy with NULL in media_cb array > >above) > > >if (media_cb[media_event][media_state] != NULL) > > media_cb[media_event][media_state](x,y,z); > > From the readability I'd go for the dummy function. > > Using ARM mode does not add a branch since the call could be > conditional. > > What core does the code run on ? Do you have caches ?
It runs on a ARM9, not sure about the cache bit. Regards

The 2024 Embedded Online Conference