gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > Function Call Overhead

#27018 - ScottLininger - Fri Oct 01, 2004 6:04 pm

I'm working on an RPG engine that fires "events" when certain interactions occur. I'm handling these event calls via function pointers.

Every "actor" in the game has several events that could potentially be fired, but many of these events will do nothing. For example, there is an OnTick event that fires every frame that the PC is touching a given actor. For 90% of the actors on the map, this event will do nothing.

So my question is this: Is it more efficient to do an IF/THEN to decide whether to call the event function (saving the function call overhead 90% of the time), or to simply call an empty function that does nothing?

The functions themselves would at most accept 2 parameters.

This is definitely a *minor* performance concern, but I'm just curious.

Thanks!

-Scott

#27019 - poslundc - Fri Oct 01, 2004 6:11 pm

It will be more efficient. You will be running through two instructions (a compare instruction that takes 1 cycle, and a branch instruction that won't be executed) versus the overhead of calling a function (context switch, branch, context switch, branch back). For 90% of the time, you may as well do the comparison instead.

But for the record, it almost definitely won't impact the performance of your game in the slightest.

Dan.

#27053 - sajiimori - Sat Oct 02, 2004 4:47 am

Yeah, definitely use the method that results in simpler code. I personally like using null function pointers to specify "no callback" -- then I can calloc an object and not worry about setting everything to their various defaults. Fewer opportunities to screw up, you know. =)

#27736 - Marill - Wed Oct 20, 2004 6:54 am

there should be not that much overhead with a function call via a function pointer. granted a if else will be more efficient.

it really depends on how you want to use this.

with the function pointer calls, it may make it eaiser for you to design your NPC events, and provides you with more flexibility to change thier behaviour by setting these function pointers in a table, thus treating it as data, rather than modifying code directly.

but again, i am jsut saying this without looking at your code, so who knows, it may be easier/more flexible in branching statements instead.

anyways, you'll just have to weigh your options. - faster more efficitnet running code, or ability to seperate behavious into data so taht they can be easily modifiable later on, just by editing data and not code.