Hudzilla.org - the homepage of Paul Hudson
Contents > Performance > Optimising your code Wish List | Report Bug | About Me ]

18.1.3     Avoid function for maximum performance

This is NOT the latest copy of this book; click here for the latest version.

Calling a function in PHP is very expensive, so avoid it whenever you can. There are two methods for accomplishing this - one for user functions, and one for built-ins.

If you are calling a user function that is quite short, consider inlining it. This is a technique employed in C and C++ that essentially copies the contents of a function to wherever it was called, thus avoiding the function call. Consider the following code example:

<?php
    
function some_func() {
        
$j = 1;
    }

    
$START = time();
    for (
$i = 0; $i < 4000000; ++$i) {
        
some_func();
    }
    
$END = time() - $START;
    echo
"Calling the function took $END seconds\n";

    
$START = time();
    for (
$i = 0; $i < 4000000; ++$i) {
        
$j = 1;
    }
    
$END = time() - $START;
    echo
"Inlining took $END seconds\n";
?>

As you can see, the function call is quite simple - only one line, with no parameters being passed in and no return value being use. On my test machine, the first loop took 15 seconds and the second loop to just 5 seconds. Having longer functions decreases the impact of function calls, but adding parameters and return values will increase it. For example, editing the above script so that some_func() takes just two parameters (although the function did not use them) increased the function loop execution time to 30 seconds - a huge difference from the 5 seconds with inlining.

For built-in functions, your only choice is to try to live without the function call. It is never better to try to rewrite a built-in function using PHP so that you can inline it, as the built-in function uses highly optimised C code that is likely to be as fast as it can get. However, there are some functions you can live without quite easily, such as floor(). This function rounds a floating-point number down to the nearest integer, which is also what typecasting a float to an integer does. In this situation, typecasting a floating-point number to an integer as opposed to using floor() is about a third faster, so it's best to avoid floor() in tight loops.

Author's Note: Potentially above and beyond all the other performance tweaks presented here, this one is definitely one best left until the very end - if you really need that extra bit of performance. Functions are there to centralise code and are incredibly helpful, and inlining them before you really need to definitely counts as premature optimisation.

Eric Raymond, in his marvellous book "The Art of Unix Programming" (available online at this link but really worth buying), has a wonderful quote from Steve Johnson, an early Unix hacker responsible (amongst other things) for the Portable C Compiler and Yacc. The quote is this: "Dennis Ritchie encouraged modularity by telling all and sundry that function calls were really, really cheap in C. Everybody started writing small functions and modularizing. Years later we found out that function calls were still expensive on the PDP-11, and VAX code was often spending 50% of its time in the CALLS instruction. Dennis had lied to us! But it was too late; we were all hooked..."

Expensive, yes, and expensive still, but there's a reason using functions is so addictive!





<< 18.1.2 Use your tools wisely   18.1.4 Use the Zend Optimizer >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Nick Tompson - 05 Dec 2008

This is a pathetic example to make the point. As has been said, calling a function 4000000 times is pretty pointless and stupid.

If you can't use functions in PHP, it's useless.

phoenix@firegod.de - 05 Dec 2008

he doesn't say "don't use functions"

it just means "don't use functions where you need maximum performance". and yes there are situations where functions are called 4 million times and more.

of course using functions is essential for minimum redundance and a good code-structure.

i've tested this on my server and added an include to see what happens:

inline: 7 sec
function: 28 sec
include: 208 sec

for just 1 little return $a+$b; function and a $c=1+2; include...

Guna - 05 Dec 2008

Programming without functions!!!

Common task in a project are wrapped and localized by function. Executed whenever necessary, but not 4000000 times.

Do you suggest "do not use function"?

A PHP User - 05 Dec 2008

Who in the right mind would call a function 4 million times.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is six plus nine?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow