I was wondering which code is effective. Notice, that $type = 1
is same during the whole loop.
if(!$handle = opendir('.')) {
return false;
}
$result = array();
while(false !== ($entry = readdir($handle))) {
if(!$resDir = self::getUrlByType($type, $entry)){
continue;
}
switch($type) {
case 1:
/* do something */
break;
case 2:
/* do something different */
break;
}
}
closedir($handle);
I think, that these both codes should be proceesed with same duration, because it is only one condition there.
if(!$handle = opendir('.')) {
return false;
}
switch($type) {
case 1:
$result = array();
while(false !== ($entry = readdir($handle))) {
if(!$resDir = self::getUrlByType($type, $entry)){
continue;
}
/* do something */
}
break;
case 2:
$result = array();
while(false !== ($entry = readdir($handle))) {
if(!$resDir = self::getUrlByType($type, $entry)){
continue;
}
/* do something different */
}
break;
}
closedir($handle);
I made negative microtime footprint at the begining of each script $time = -microtime(true);
and the difference between current microtime $time += microtime(true);
at the end of script. After i displayed the results echo "Time: ", sprintf('%f', $time), "
";
i saw these values:
Time of first script was average between 0.0084
and 0.0203
.
The secon results were much different, average between 0.0062
and 0.0072
.
My question is, what is the case of this behaviour? Why didnt PHP compiler cashed that?
This ist the PHP output:
Type: 1
Type: 1
Type: 1
Type: 1
Type: 1
Type: 1
Type: 1
Type: 1
Time: 0.000103
Type: 1
Type: 1
Type: 1
Type: 1
Type: 1
Type: 1
Type: 1
Type: 1
Time: 0.000065