Most efficient code, yar.

Ahoy Me LASSY,

Which of the two codes would be most efficient? having a conditional statement inside the loop favoring length, or having the conditional statementsd outside of the loop… yarr yaar yaaaaar yaaaaaaaaaaaar

This is peusedo code, for something im working on btw.

<?php


function xxxx($xxx) {
	foreach ($xxx as $xyy){
		if ( $xxx && $xxy && $xyy ) {
			// do something
		}else{
			//do something else
		}
	}
}


function xxxx($xxx) {
	if ( $xxx && $xxy && $xyy ) {
		foreach ($xxx as $yy){
			//do something
		}
	}else{
		foreach ($xxx as $yy){
			//do something else
		}
	}
}

?>

ahoy!!! forgot to put in the TAGS! YEHAWW

I’d say the if statement outside the loop is more efficient. That way the if statement will only have to be evaluated once, and not with each iteration of the loop. You could do some benchmarking with microtime() and 100,000 iterations if you wanna test it yourself, but I’m predicting the if statement outside the loop to be faster.

Sponsor our Newsletter | Privacy Policy | Terms of Service