10
11

More than 5 years have passed since last update.

【PHP】処理にかかる時間を計測して時間・分・秒で表示

Posted at

まずはじめに

【PHP】割り算の切り上げ、切り捨て、四捨五入の続きです。
もっと言えば、上記の記事の内容を調べるきっかけになったものです。

処理する時間を計測して時間・分・秒で表示

hoge1.phpの中で『0から1000までの数を表示させる処理をするhoge2.php』を呼び出すことで今回は計測しました。

プログラム

hoge1.php
<!DOCTYPE html>
<html lang="ja">
    <head>
        <meta charset="utf-8">
        <title>処理時間を計測</title>
    </head>
    <body>
        <?php
            $link_time = time();
            require('hoge2.php');
            $time = $get_time - $link_time;
            if($time>=60){
                $minute = floor($time/60);
                $second = $time - $minute*60;
                if($minute>=60){
                    $hour = floor($minute/60);
                    $minute = $minute - $hour*60;
                    echo "<p>TIME:".$hour."[h]".$minute."[min]".$second."[s]</p>";
                }else{
                    echo "<p>TIME:".$minute."[min]".$second."[s]</p>";
                }
            }else{
                echo "<p>TIME:".$time."[s]</p>";
            }
        ?>
    </body>
</html>
hoge2.php
<?php
echo "<ul>";
for($i=0;$i<=1000;$i++){
    echo "<li>".$url."</li>";
}
echo "</ul>";
$get_time=time();

解説

解説というほどでもないのですが、
まずはじめにhoge1.phpが呼び出された時間をtime()関数を使って取得して$link_timeに格納しました。hoge2.phpを呼び出して、次に1から1000までを計算をした後、$get_time=time();として処理が終わった時間を取得。
そして、$get_timeから$link_timeを引くことで処理時間を取得しました。

今回は秒までの処理時間しか考えない予定だったので、
microtime()関数は使わなかったです。

10
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
11