PHP Sadness

(dubious) Function calls have enormous overhead (15x perl)

This sadness is marked 'dubious' because different people seem to be getting wildly different results than mine, even though my results below are representative of experimentation on a variety of versions and platforms. This sadness clearly needs a better metric.

In PHP, function calls are depressingly slow. This is anti-functional because you cannot break your code into logical functions in large applications, where the mere cost of the function itself will noticably slow down the application and degrade the user experience. Discouraging the use of functions prevents developers from building clean, reusable code in an efficient way and teaches beginners bad habits.

In PHP 5.2.9:

$ php -r 'function a(){} $a=microtime(TRUE); for($i=0;$i<1000000;$i++){a();} var_dump(microtime(TRUE)-$a);'
float(5.3318428993225)
$ perl -e 'use Time::HiRes qw(time); sub a{} $a=time(); for($i=0;$i<1000000;$i++){a()} print +(time()-$a)."\n"'
0.451832056045532
$ php -r 'function a(){} $a=microtime(TRUE); for($i=0;$i<1000000;$i++){} var_dump(microtime(TRUE)-$a);'
float(0.20932388305664)
$ perl -e 'use Time::HiRes qw(time); sub a{} $a=time(); for($i=0;$i<1000000;$i++){} print +(time()-$a)."\n"'
0.107325077056885

For one million iterations, it takes PHP .21 seconds to loop, and Perl .11 seconds.

For one million function calls, it takes PHP 5.33-.21 = 5.12 seconds, and Perl .45-.11 = .34 seconds.

This puts PHP at 15x slower.

In PHP 5.3.2:

$ php -r 'function a(){} $a=microtime(TRUE); for($i=0;$i<1000000;$i++){a();} var_dump(microtime(TRUE)-$a);'
float(0.65169477462769)
$ php -r 'function a(){} $a=microtime(TRUE); for($i=0;$i<1000000;$i++){} var_dump(microtime(TRUE)-$a);'
float(0.0794358253479)
$ perl -e 'use Time::HiRes qw(time); sub a{} $a=time(); for($i=0;$i<1000000;$i++){a()} print +(time()-$a)."\n"'
0.175878047943115
$ perl -e 'use Time::HiRes qw(time); sub a{} $a=time(); for($i=0;$i<1000000;$i++){} print +(time()-$a)."\n"'
0.0536401271820068

PHP took .573 seconds, while Perl took .122 seconds, putting PHP at 4.7x slower.