How to use ScheduleUpdate function better?

In situation like this. I have one counter which will be activate every one second, one counter every three second, and a update() function to update every frame.

first case:

schedule(OneSecFunc, 1);
schedule(ThreeSecFunc, 3);
scheduleUpdate();

or second case:

void update(float dt)
{
oneSecCounter += dt;
threeSecCounter += dt;
if(oneSecCounter >= 1)
do one sec thing…
if(threeSecCounter >= 3)
do three sec thing

do the rest
}

Which case is better in performance? or are they the same?
Thanks in advance :laughing:

The first one, as the code does not have any branches, but maybe the schedule function and the update function itself run some branches and this makes it hard to tell.

Just profile them. It depends on various other factors as well. E.g, how good your CPU is at branch prediction or how many cache misses your code generates.

1 Like

thank for your suggestion :smile:, to tell you a truth i dont have much knowledge about cache misses, but with your answer, if the code in function in case one is simple and not much branch prediction, it will be better for coding practice, that’s all i want to know now. Thanks!

Branches are evil. Avoid them whenever possible. Well, in fact it is possible to get rid of all branches as every algorithm/code can be implemented branch-less.

1 Like