9
8

More than 5 years have passed since last update.

PHPで実行時間をベンチマークするスニペット

Last updated at Posted at 2015-01-15

スニペット

    function b() {
        static $start;
        if (!$start) {
            $start = microtime(true);
            return;
        }

        static $count = 0;
        $count++;

        $now = microtime(true);
        printf(
            '%04d : %s ms (%sMB)<hr/>', 
            $count, 
            number_format(ceil(($now - $start) * 1000)), 
            number_format(ceil((memory_get_usage() / 1024 / 1024)))
        );
        $start = $now;
    }

利用例

b();
slowFunction1();
b();
slowFunction2();
b();

出力例

0001 : 2,334 ms (5MB)
0002 : 520 ms (5MB)

まとめ

XhprofとかNew Relic使った方がいい。

9
8
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
9
8