Python vs. PHP
I have been programming for about 7 years now off and on and I feel I have let myself down by not really following through on one single language. So I said to myself that Python will be the language I will concentrate on learning inside and out.
This morning I created two script, one in Python and another in PHP. Each script loops through a FOR statement 2,000,000 times calculating the MD5 hash of N + N, N equaling the number of passes thus far. It is a very basic script showing a quick speed comparison between the languages.
Here are the two scripts: PHP, Python
and the results
Round 1 - 2,000,000 Passes
PHP = 21.4227 sec
Python = 9.8737 sec
Round 2 - 2,000,000 Passes
PHP = 21.1122 sec
Python = 9.7241 sec
Round 3 - 1,000,000 Passes
PHP = 9.811 sec
Python = 4.429 sec
Round 4 - 1,000,000 Passes
PHP = 9.857 sec
Python = 4.280 sec
As you can see, Python is more than 2 times faster than PHP in performaing this operation. I really enjoy writing PHP, but I think for any web applications I write that need to interface with a standalone desktop application or anything that needs real power I will choose Python.






May 5th, 2007 at 11:44 am
Very cool, thank you for the benchmark.
May 26th, 2007 at 12:58 pm
When comparing two platforms for Web development, you should concentrate more on what is relevant for the Web development, and number crunching abilities are not very important in this field. A more interesting test would be for instance how many pages can be served within a given time frame by each framework (PHP vs. some Python Web framework) - I think you’ll see that in this case the numbers are reversed.
May 26th, 2007 at 1:10 pm
one more thing: lest it would appear that page throughput is the most important factor to look for in a Web framework: I’m not saying that PHP is definitely more suitable than Python for writing Web applications (as always, there are many factors and any general claim would be wrong), only that this particular benchmark is not what I would suggest basing a decision on
May 26th, 2007 at 4:12 pm
I agree that general claims can be bad, there are a number of benchmarks that be performed in one language that will perform better than benchmarks in another, but at the same time you may have 5 more that perform better in the 2nd than they did in the first.
I may do a benchmark on the serving of web pages now that you mention it. I would be interested to see how PHP and Django or mod_python stack up. Here is a link that you can compare languages using a number of benchmarks.
At the time I wrote this little benchmark I was looking for a powerful solution to crunch backend data that was more independent of the web part (REST, cmdline, etc). These are benchmarks on the languages themselves. When you are going to benchmark the actual web development part you would have to throw in a lot of factors. The web server, what modules are handling your PHP and Python, the versions, etc. You would also have to compare PHP in OO form and outside OO form to determine how the OO stacks up.
I mention at the bottom that I would choose python when I needed real crunching abilities on the back end because the power it has. If Python can crunch something in half the time I could easily call the Python script from PHP and it would complete the operation faster giving PHP the ability to pump out more pages. The other thing I mentioned was writing to interface with a standalone application because the script that would be doing the crunching for PHP could easily integrate into any Python based GUI/console application written in Python. That is if I did not want to write the entire site in Python.
You make good points but the purpose of this benchmark was a little different, I failed to make that clear, for that I apologize.
May 26th, 2007 at 5:29 pm
point taken - I failed to read the last sentence properly and for that I apologize
May 26th, 2007 at 5:58 pm
I’m glad you commented, you made really good points and I really havn’t seen too many comparisons on the scaling abilities of each language. You hear about this language scales well and that language scales well, and while I havn’t really looked for any supporting data, I havn’t seen a whole lot. Google is awesome with Python, but if you have a cluster of hundreds/thousands of computers I think a lot of languages would be awesome. vBulletin has been a leader in the forum world for a long time and it is based on PHP, yet some say PHP sucks. When I build this other dev server I am working on maybe I will work on doing a Rails, Django, PHP, mod_python comparison.
Thanks David