LoginSignup
5
2

More than 5 years have passed since last update.

ベンチマーク : public static function __callStatic($method, $args)

Last updated at Posted at 2017-06-26

目的

マジックメソッドは遅い遅い言われていたので、フレームワークなどでよく使われる__callStaticはどれくらい遅いか試してみた。

実行コード

<?php

class A
{
    public function test()
    {
        return 1.5 * 100 * 10;
    }
}

class Test extends A
{
    public static function __callStatic($method, $args)
    {
        return (new self)->$method($args);
    }
}

$start = microtime(true);

for ($i = 0; $i < 1000000; $i++) {
    Test::test();
}

print microtime(true) - $start . PHP_EOL;

$start = microtime(true);

for ($i = 0; $i < 1000000; $i++) {
    (new A)->test();
}

print microtime(true) - $start . PHP_EOL;

結果

// call staticの場合
1.6370959281921
// 通常呼び出しの場合
1.0607688426971

結論

5割り増しぐらい。
元々の関数呼び出しが早いのでそんなに気にするほどの遅さではないですね。
参考材料にでも。

5
2
7

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
5
2