2

Everyone knows that function calls in PHP hit the performance badly. This script demonstats the problem:

// Plain variable assignment.
    $time = microtime(true);
    $i = 100000;
    while ($i--)
    {
        $x = 'a';
    }
    echo microtime(true) - $time."\n\n";
// 0.017973899841309

    $time = microtime(true);
    function f() { $a = "a"; return $a; }
    $i = 100000;
    while ($i--)
    {
        $x = f();
    }
    echo microtime(true) - $time."\n\n";
//0.18558096885681

By the way anonymous functions are the worst. thy are 10 times slower.

Is there a PHP-Script-Optimizer that reduces the amount of function calls and minifies the script?

There is also this post: Why are PHP function calls *so* expensive? related to this article

Community
  • 1
  • 1
  • 1
    You comparision is a bit unfair, because your function call case is doing more work than just adding the function call. The function body should be $x='a'; return; the function call should be just f(); and your microtime start call should occur after the function declaration, not before. But agreed, PHP is pretty slow at function calls. In fact, its pretty slow at a lot of stuff, because of its implementation. Why pick on function calls? – Ira Baxter Jun 20 '13 at 13:59
  • I tested your exact code with APC enabled and it is only about 2.5 times slower to use the function calls. – Pitchinnate Jun 20 '13 at 14:02
  • All what you're talking about is called **Premature optimization** – Yang Oct 22 '13 at 17:59

1 Answers1

0

You only really call functions that you need at any given time, therefore no.

The thing you could do to optimize your code is using as minimal anonymous functions, reducing the amount of spaces (e.g. use a php minifier) and rename your functions to 1 letter names,

this would atleast tokenize your script which would allow for faster reading of the functions. But in terms of optimalisation you are better off not doing so due to the readability being completely gone down the drain.

SidOfc
  • 4,552
  • 3
  • 27
  • 50