Static methods and performance

March 28th, 2005. Tagged: PHP certification

How fast/slow are the static methods?
I did a test once to try and figure that out.

It turns out:

  • Calling a method statically is just a bit faster than calling it "normally"
  • Calling a static method is twice as fast if your object didn't exist to begin with
  • Static methods are twice as slow than function calls

By calling statically I mean:
< ?php
Some_Class::someMethod();
?>
An calling "normally" is when you create an object and call the method using ->
< ?php
$sc = new Some_Class();
$sc->someMethod();
?>

Tell your friends about this post: Facebook, Twitter, Google+

3 Responses

  1. I like to point out, that this benchmark, has a flaw in it for more modern PHP versions. PHP 5.3 tested at 20/10/2010, vs the original benchmark of 2005. But as this post shows up as a top search results in Google on keywords, maybe its better to fix a small misconception.

    The original code call’s the class function, with no “static” keyword declaration to the function. In today’s development, this shows a large performance difference.

    [code]

    class Test {
    function testMethod()
    {
    return 2 + 2;
    }
    }

    class sTest {
    static function testMethod()
    {
    return 2 + 2;
    }
    }

    [/code]

    Calling this as:

    Test:: testMethod();
    sTest:: testMethod();

    “sTest” will be 0.016328 while “Test” will be 0.06599. Results are similar in different tests ( done on a slow Atom330 ).

    The results make that:

    First loop: 0.016328 0.06599 0.013621 0.046693 0.009572
    x loops: 0.016528 0.065566 0.013492 0.046129 0.009499

    Pure Function call, is of course as expected, the clear winner with 0.009x.
    Second fastest is Method with Object creation, with 0.013x range.
    Third fastest is Static Call, with static keyword next to the function, with 0.016x range.
    Fourth is the Method with object creation inside the loop, with 0.046x range.
    Fifth is the Static Call, on a normal method! with 0.065x range.

    In summary:

    The performance difference between a object what has been created outside the loop, or directly calling the static object ( with static keyword! ) is very limited. 0.013x vs 0.016x is very small, and shows how fast static calling actually is.

    Calling a method as a static, without the static keyword, will results in a almost massive 4* overhead.

    I hope this helps people that stumble on this article, and will prevent them from doing incorrect coding.

  2. My tests reveal the following results:

    CPU: i7 (860) 2.79GHz
    RAM: 8 GB

    OS: Windows 7 Ultimate (64-bit)
    PHP: 5.3 running on Apache

    Iterations: 1,000,000

    class Test {
    public static function something1() {
    return 2 + 2;
    }

    public function something2() {
    return 2 + 2;
    }
    }

    // Test #1 – Static Calls
    // Average: 0.000,79 ms / 0.000,000,79 s
    for ($i = $samples; $i > 0; –$i) {
    Test::something1();
    }

    // Test #2 – Object Member Call
    // Average: 0.001,40 ms / 0.000,001,40 s
    for ($i = $samples; $i > 0; –$i) {
    $test_obj->something2();
    }

    // Test #3 – Instantiate Object and Member Call
    // Average: 0.002,60 ms / 0.000,002,60 s
    for ($i = $samples; $i > 0; –$i) {
    $test = new Test();
    $test->something2();
    }

  3. Most Business Knows How to Use Google ,But Most DO Not Know How their Businesses SHOULD Utilize Google to its Benefit \

Leave a Reply