Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

python performance...am I missing something?

Avatar of Steve Bink
Steve BinkFlag for United States of America asked on
PHPPython
12 Comments1 Solution508 ViewsLast Modified:
The background for the specific test case is here: https://www.experts-exchange.com/Programming/Languages/Scripting/Python/Q_27781765.html.  

I ran some benchmarks to find the best performer among several methods, and was getting back some astounding results.  Running the same code in PHP yields much better performance.  I've previously read that PHP-vs-Python is a bit of a mixed bag, with Python performing better in data handling, and PHP performing better with I/O.  These results are showing quite a different picture, and the Python code is not even executing the type conversion.  Can anyone point out where I've gone wrong?

For Python:
$> python
Python 2.6.6 (r266:84292, Sep 12 2011, 14:03:14)
[GCC 4.4.5 20110214 (Red Hat 4.4.5-6)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> def tryit():
...   tin=time.time()
...   a="97757589545391667"
...   for x in range(1,10000001):
...     try:
...       b=a
...     except:
...       pass
...   tout=time.time()
...   print tout-tin
...
>>> tryit()
1.29326200485
>>> tryit()
0.987454891205
>>> tryit()
0.976771116257
>>> tryit()
0.977509975433
>>> tryit()
1.36146092415

For PHP:
$> php -v
PHP 5.3.3 (cli) (built: May  3 2012 17:33:17)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies


$> php -r '$in=microtime();$a = "97757589545391667";for ($x=0;$x++;$x<10000001) { $b=(int)$a;} $out=microtime();echo number_format($out-$in,8);'
0.00006000

$> php -r '$in=microtime();$a = "97757589545391667";for ($x=0;$x++;$x<10000001) { $b=(int)$a;} $out=microtime();echo number_format($out-$in,8);'
0.00006700

$> php -r '$in=microtime();$a = "97757589545391667";for ($x=0;$x++;$x<10000001) { $b=(int)$a;} $out=microtime();echo number_format($out-$in,8);'
0.00008900

$> php -r '$in=microtime();$a = "97757589545391667";for ($x=0;$x++;$x<10000001) { $b=(int)$a;} $out=microtime();echo number_format($out-$in,8);'
0.00006000

$> php -r '$in=microtime();$a = "97757589545391667";for ($x=0;$x++;$x<10000001) { $b=(int)$a;} $out=microtime();echo number_format($out-$in,8);'
0.00005900

$> php -r '$in=microtime();$a = "97757589545391667";for ($x=0;$x++;$x<10000001) { $b=(int)$a;} $out=microtime();echo number_format($out-$in,8);'
0.00006000
ASKER CERTIFIED SOLUTION
Avatar of gelonida
gelonidaFlag of France image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 12 Comments.
See Answers