-PHP Tips- 第3回 ForとWhile

ForでもWhileでも一緒じゃないの?
【Tips】whileの方が若干高速!
【Description】数%ながら有意な差が認められる程度、whileの方が高速

<?php
define
("WORK_TIME", 20000);

function getmicrotime(){
    list(
$usec, $sec) = explode(” “,microtime());
    return ((float)
$usec + (float)$sec);
}

$src = “hoge”;

//Forの場合
$start_time = getmicrotime();
for(
$i = 0; $i < WORK_TIME; $i++){
    
$dummy = $hoge;
}
$for_time = sprintf(“%.4f”, getmicrotime() – $start_time);

$i = 0;

//Whileの場合
$start_time = getmicrotime();
while(
$i < WORK_TIME){
    
$dummy = $hoge;
    
$i++;
}
$while_time = sprintf(“%.4f”, getmicrotime() – $start_time);

?>
<html>
<head><title>ForとWhileの比較</title></head>
<body>
Forの結果:<?php echo($for_time);?>秒<br>
Whileの結果:<?php echo($while_time);?>秒<br>
</body>
</html>
</code

タイトルとURLをコピーしました