I am micro-optimizing this function and have a bit of a problem where inside a loop I am checking if a value outside of the loop is 0, if so call function and similarly in the function it calls. How would I refactor it so there is no conditional logic (which is slow) inside these loops.
foreach($this->layer[$l]->objs as $obj)
{
//Horrific code to save 20ms :(
($l===0) ? $obj->myfunc_clean($var,$var2) : $obj->myfunc($var,$var2);
}
I need to get rid of the condition $l===0
because this condition in a loop of thousands of objects slows it down considerably. Also I have function that needs to process as normal but the other needs to unset the temp variables when it finishes (based on l being 0). The myfunc
function also has a loop over yet more objects hence why I am having to call separate functions to save yet more conditional logic.
Note: This may look like premature optimization but trust me, for my application, saving one millisecond can save precious seconds (probably around 10,000 iterations if not more). So please no answers about premature optimization is the root of all evil and whatnot. This is certainly not premature.