define('C_DECIMALS', 7); // 小数点以下の桁数
// for文のテスト
// 現在の開始時間を格納
$startTime = getMicrotime(); // PHP4,5 互換
//$startTime = microtime(true); // PHP5 以上
// 計測したい処理を書く
$hoge = 0;
for ($i = 0; $i < 1000; $i++){
$hoge += $i;
}
// 計測完了時の時間を格納
$endTime = getMicrotime(); // PHP4,5 互換
//$endTime = microtime(true); // PHP5 以上
// [終了した時間]と[開始した時間]の差が実際に掛かった時間。
echo number_format($endTime - $startTime, C_DECIMALS) . '<hr>';
// while文のテスト
$startTime = getMicrotime(); // 現在の開始時間を格納
// 計測したい処理を書く
$hoge = 0;
$i = 0;
while ($i < 1000){
$hoge += $i++;
}
// 計測完了時の時間を格納
$endTime = getMicrotime(); // PHP4,5 互換
// [終了した時間]と[開始した時間]の差が実際に掛かった時間。
echo number_format($endTime - $startTime, C_DECIMALS) . '<hr>';
/*
* 現在の時間を、マイクロ秒単位で取得
* PHP4, 5 互換
* PHP5 以上なら、 microtime(true); で同じ結果が取得できる
*/
function getMicrotime()
{
list($msec, $sec) = explode(" ", microtime());
return ((float)$sec + (float)$msec);
}
ネタ元