I've heard that Python is meant to be faster than PHP in terms of Runtime, I simply took this as a given and sat down today to make a blog post about it. After being told by Vinko Vrsalovic how to time scripts I took converted some code for getting prime numbers into Python and PHP then ran each 3 times and recorded the numbers. All times are in seconds.

Python => 144.829, 144.771, 144.862 (Average 144.8206)
PHP    => 102.783, 100.707, 100.663 (Average 101.3843)

I tried 3 different methods of storing the output in Python but they made a difference of approximately 2 seconds and when both scripts were set to output the data as soon as they got it rather than all at once the results were also only a few seconds off the above numbers.

Was all the stuff I heard about Python being faster wrong or have I done something appalling with my Python code?

Here is the Python code

#!/usr/bin/env python
primeNumbers = []
output = []

for i in xrange(2, 100000):
	divisible = False

	for number in primeNumbers:
		if i % number == 0:
			divisible = True

	if divisible == False:

print ''.join(output)

And here is the PHP code

#!/usr/bin/env php
$primeNumbers = array();
$output = '';

for ($i = 2; $i < 100000; $i++)
	$divisible = false;

	foreach ($primeNumbers as $number)
		if ($i % $number == 0)
			$divisible = true;

	if ($divisible == false)
		$primeNumbers[] = $i;
		$output .= $i;

echo $output;

All tests were run with the following command and under near identical conditions

$ time ./script.ext


google.co.uk/… - We rank 5th!

Written by Teifion

Python is faster and requires less memory most of the time stackoverflow.com/questions/62333/… All artificial benchmarks are evil.

Written by J.F. Sebastian

IMO the question should not have accepted answer due to we can construct benchmarks where php is faster then python and vice versa.

Written by J.F. Sebastian

It keeps surprising me that we as devs always want the best possible performance especially when the performance clearly is CPU/Mem bound. Python, PHP and Ruby are ALL more than fast enough for 99.9% of the usecases. But hey, I couldn't resist looking this question either.

Written by Bob Fanger

The main thing slowing Python down here is probably the global variables. But it is true that CPython is slowish at integer arithmetic, compared to other tasks, because in CPython even an integer is allocated in the heap and reference-counted.

Written by Jason Orendorff

Yep, if you just throw all that in a function and call it, it's roughly twice as fast, on my machine.

Written by Jason Orendorff

@Teifion: Could you please, please, please remove the answer tick from this completely unfair benchmark? It let's PHP appear in a pretty bad light only because the author was ignorant (intentionally or not).

Written by NikiC

Accepted Answer

I actually did a benchmark with a few simple items, mostly to do with split strings and array manipulation. I did it with Python, PHP, Ruby and Perl.

What I wanted to accomplish was to see how much faster PHP was than Ruby, and if PHP was faster than Perl, why I did Python was just because I could :)

What I found out is that Python actually was the fastest by a large factor, and Ruby was more than twice as fast as PHP, and PHP totally failed on 1.000.000 lines while everything else ran just fine.

Here is the result

$ time php readArray.php

real    5m1.741s
user    5m1.086s
sys 0m0.640s

time ruby readArray.rb
$ time ruby readArray.rb
Stærðin á fylkinu er : 1000000

real    0m0.541s
user    0m0.487s
sys 0m0.052s

time perl readArray.pl
$ time perl readArray.pl
Size of array is 1000000

real    0m0.726s
user    0m0.565s
sys 0m0.161s

$ time python readArray.py
my arr is :

real    0m0.179s
user    0m0.126s
sys 0m0.053s

Here is the code for PHP:

$file = file_get_contents ("testfile");
$fylki = split("\n", $file);

print "Count is :".count($fylki)."\n";


And ruby:

a = File.read("testfile")
myarray = a.split("\n")
puts "Arraysize is : " << myarray.size.to_s

And Python:

a = open("testfile").read() # or just open("testfile").readlines() # it keeps '\n'    
myarr = a.split("\n")
print "my arr is :", len(myarr)

So my conclusion was that PHP was not as fast and powerful as the rumor is, and Ruby is not slow at all, which was exactly what I as a PHP guy did not want to see.

But being a Ruby fan this convinced me to look better at Ruby. I have no idea why I am not turning to Python, as it was the fastest by a large margin and being quite simple, I have no idea.

P.S.: Just added the Python time.

This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki