Jump to content

[SOLVED] PHP vs Python


The Little Guy

Recommended Posts

OK, I ran 2 Scripts.

 

PHP:

<?php
$p = 10000000;
$t = 0;

$s = microtime(true);
for($n=0;$n < $p;$n++){
$t = md5($n + $n);
$t = 0;
}
$e = microtime(true);
echo $e-$s;
?>

 

Python:

#!/usr/bin/python 

import md5
from time import clock

p = 10000000
n = 0
t = 0
start = clock()
while n < p:
t = n + n
md5.new(str(n)).digest()
n = n + 1
t = ''
stop = clock()
print "Content-type: text/html\n\n"
print round(stop-start,4)[/coe]

 

The final outputs (rounded to the nearest 100th):

PHP:

- 30.84

- 25.66

- 21.90

- 24.75

- 19.25

 

Python:

- 23.89

- 23.25

- 23.46

- 23.44

- 23.45

 

Averages:

PHP: 24.48

Python: 23.49

 

WOW, I really thought Python was going to win by a lot, since PHP's First round was almost 31. Guess I was wrong.

 

Python does look like a more constant language though seeing that all the results stayed at 23, otherwise PHP just caches the values for faster speeds when ran multiple times.

 

Any thoughts on this test?

Link to comment
Share on other sites

My results:

 

#PHPPython

143.6428.60

244.0827.66

343.7228.34

444.4527.81

544.0728.49

643.5127.97

743.5628.00

844.6028.52

944.6627.86

1044.2827.80

Avg:44.0628.11

 

Note no reduction in time for consecutive PHP executions.

 

It's run on Vista Business, Intel CoreDuo T9500

 

Link to comment
Share on other sites

FYI, the second time you run the python script it should be faster because it caches the byte code so it doesn't have to be interpreted again unless the source file has been changed. Those are the .pyc files you'll see. You can achieve the same effect with an opcode cache for PHP such as APC as well of course.

Link to comment
Share on other sites

No, but on a large scale project with many files, having an opcode cache can improve the performance considerably. Besides, if we say that we run a small script like the above ones a thousand times then you should see a noticeable difference by saving 999 interpretations. A site like The Pirate Bay gets many thousands of hits each minute so caching is crucial for system performance regardless of the script's complexity.

Link to comment
Share on other sites

The issue in question was whether Python was faster than PHP. I suppose this is for real-world usage instead of tiny benchmarks only covering a confined amount of structures and features of each language in a sample script that wouldn't be used otherwise. The only thing tested in the above benchmark was loops, integer addition and the MD5 algorithm.

Link to comment
Share on other sites

I don't think there is a fair comparison that isn't long and drawn out.  Basically you would have to create the same application in both languages, making sure everything is handled the same.  Even then though, it could be inaccurate, although it would give a real world reading. 

Link to comment
Share on other sites

Python is almost 3 times slower on my box...  Like already mentioned, this is a horrible comparison.  For example, what if the md5 function is just super slow on my computer under python?

 

 

I just realized what I typed earlier, and I want to reword that to say:

 

Python is almost 3 times slower on my box with this script...  Like already mentioned, this is a horrible comparison.  For example, what if the md5 function is just super slow on my computer under python?

Link to comment
Share on other sites

Once again, I can't/won't vouch for it being accurate/true.

 

Since mysqlnd is not a finished product yet (or so I understand) then of course all benchmarks have to be taken with a grain of salt.

Nevertheless it's nice to see some. Thanks for the links.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.