Recently I have needed to run a cron job every minute on a high load web server. As am currently most fluent in PHP I naturally wrote my script in PHP. However, as it's running every minute, I do want it to be as fast as possible. Seeing as the script was very simple, I thought that the most time required to run is in loading the PHP interpreter. Maybe i could use a compiled language instead? What about Google's new language Go?
Anyway, for my test I wrote a "hello, world" command line app in each language and ran the program 1000 times. Without any further ado, here are the results, from fastest to slowest:
C: 5.37 seconds
Go: 6.23 seconds
Perl: 7.97 seconds
Python: 20.64 seconds
PHP: 41.31 seconds
There you go! PHP is a bit slow for this kind of thing! But I'm not going to write all my scripts in Go or C now (what a hassle!). So, for me…. perl wins!
Ahh, good old perl, my faithful friend! I will never forget you!
Does your web server have mod_perl compiled into it? That could be the reason.
Greg, I'm not running any of these tests through a web server at all – only from the command line.
Interesting point about mod_perl though – it reminds of when I switched from Perl to PHP for web apps years ago. PHP was so much faster because the interpreter was build into apache (mod_php). Perl had a horrible delay with each request as the perl interpreter had to be loaded with each request. mod_perl was the answer – but when i tried to port my apps to it – disaster! Not thread safe!
When I get time I'll run these simple hello world tests again through apache.
Yes, running a script as a cron job, does not run through the web server. These are generally used for update/cleanup methods that run outside of the web server. Interesting about mod_perl though, is that most people tend to use the Registry method. I've found creating your own handlers to be way more efficient and fast. Plus, it gives you far more power over the entire request that you can't get with PHP. You can do everything from programmatic url rewriting to remapping requests to specific files. For example, I wrote a handler that would handle file uploading in a more correct fashion, as it allows me to cancel posting while in progress. Something PHP can't do, you have to wait until the entire post is done before your script fires.
You are right though, about the thread safe. Although, most Apache installations are still MP and not MPMT or MT. Most scripts people write for Registry are not MP safe.
There must be some per-process overhead with your Python binary that doesn't exist with the Perl one… This overhead, which happens only once per script execution, is hardly significant unless you run a script many times per second. Start-up time aside, Python is generally a bit faster than Perl.
Phyton is not faster as Php, you are wrong. Maybe you have a bad compiled php version or some bad module.
Sure, why don't you run your own trials and post the results.
And remember people, I'm talking about command line scripts here.
Can't people read anymore or something?
Very enlightening test. I would have thought C would win out, however for PHP to be 8 times slower is not encouraging.
@PHP Programmer:
Actually PHP being 8 times slower than C is wildly inaccurate, probably because this benchmark is heavily weighted toward I/O, which will blur the speed differences between languages. As far as the language goes, you can expect that C would be 50 – 80 times faster than an interpreter like PHP or Python.
Hi Wilsa,
This was a real world test. How about backing up your 'theories' with some practical tests of your own before spouting out "wildy inaccurate" crap.
The biggest advantage of PHP over Perl is that PHP was designed for scripting for the web, while Perl was designed to do a lot more. Because of this, Perl can get very complicated. The flexibility / complexity of Perl can make it difficult for developers of varying skill levels to collaborate. PHP has a less-confusing and stricter format without losing flexibility. PHP is also easier to integrate into existing HTML than Perl. In large part, PHP has all the 'good' functionality of Perl – constructs, syntax, et cetera – without making it as complicated as Perl can be. Yet PHP's command-line interpreter (CLI) is powerful enough to perform high-level tasks much in the same way Perl has been traditionally employed. Perl is a very tried and true language, and has stood its ground since the 1980's, but PHP has matured and evolved quickly, and continues to make fantastic progress.