LoginSignup
1
3

More than 3 years have passed since last update.

PHPで処理時間を測定する(microtime)

Posted at

microtime()

microtime() という関数があり、これでマイクロ秒まで取得できる。
PHP マニュアル

<?php
// 引数を渡さないと、以下のように現時刻が取得できる。
// 0.26283300 1569114629 
microtime();

// 引数に true を渡すと float で現時刻が取得できる。
// 1569115527.2943 
microtime(true);

処理時間を測定する

microtime() でマイクロ秒の現時刻が取得できるので、後は測定したい処理の前後を比較すれば良い。

<?php

class ClassName
{

    public function execute()
    {
        // 処理前の時刻を取得
        $start = microtime(true);

        // 測定したい処理
        $this->FunctionName();

        // 処理後の時刻を取得
        $end = microtime(true);

        // 差分を表示
        echo($end - $start);
    }

    public function FunctionName()
    {
        # code...
    }
}
1
3
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
1
3