Function Calls on ARM

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

Reply to
holysmoke
Loading thread data ...

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.

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

Reply to
Wilco Dijkstra

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
Reply to
Ico

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 :-)
 Click to see the full signature
Reply to
42Bastian Schick

It runs on a ARM9, not sure about the cache bit.

Regards

Reply to
holysmoke

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.