Categories
Performance PHP Web

Run Times for PHP Function & Method Call Types

Why should we care about code performance?

Small decisions made toward code performance when totaled over the span of our careers can make a significant impact in our world. The impact is not making a program “run fast”. The impact is saving invaluable human time and bringing everyone closer to carbon-neutral.

As developers, it’s essential to consider how we put the electrons flowing through our apps to use. Each decision we make can add up (even if we’re not using a programming language built for raw speed).

When we make informed decisions in our daily work we unlock the potential to make the world a better place to live. Yes, even in the small choices we make when writing PHP code. How ridiculous is that!

A first step to making that kind of impact is found in adopting new habits into our daily work. For example, we create and call functions every day in every application we make. In PHP how can we make better choices toward performance with functions?

The results might shock you.

Caller Type100 Calls10,000 Calls1,000,000 Calls
Standard function0.176ms8.596ms612.005ms
Static class method0.200ms9.607ms731.104ms
Object method (Same Instance)0.211ms9.848ms787.301ms
Anonymous function0.274ms14.389ms1109.482ms
Object method (new instance per call)0.333ms16.658ms1295.540ms
Static class method called through __callStatic0.427ms30.995ms2495.849ms
Object method called through __call (same instance)0.427ms31.863ms2422.210ms
Object method called through __call (new instance per call)0.541ms38.024ms3020.802ms

The results here reveal that a standard function call in PHP is the most performant type of caller (1.5 times after then an anonymous function call).

// Standard Function: Fastest Caller
function my_function($a, $b) {
    return $a <=> $b;
}

echo my_function(1,2);

With this list considered, I’ve found I need to use anonymous functions a lot less (particularly when those functions are used a lot).

// Anonymous Function: Slower Caller
echo ( fn($a, $b) => $a <=> $b )(1,2);

All things considered, performance is only one variable within the body of work. Other considerations also exist. Space used, code maintainability, and a program’s lifetime value are some of those considerations. A project needs its own consideration each time we put out hands to the keys.

Regardless, making good habits toward performance is a noble goal.

P.S. Namespaces had no impact on these results when I used them. I used namespaces on functions and class definitions.

One reply on “Run Times for PHP Function & Method Call Types”

The rise of javascript seems to have popularized anonymous functions, and while I understand they have their purpose, they also seem to generally not be tested (and scattered throughout codebases they’re not easily testable). Identifiable anonymous functions that are obvious what they’re doing – yeah, maybe there’s some quick value in inlining those, but I sometimes see relatively complex logic inlined in as a closure someplace, and it’s hard to test/verify/modify with any confidence.

Leave a Reply to Michael Kimsal Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.