troubleshooting Question

python performance...am I missing something?

Avatar of Steve Bink
Steve BinkFlag for United States of America asked on
PythonPHP
12 Comments3 Solutions508 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
gelonida

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

Join our community to see this answer!
Unlock 3 Answers and 12 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 12 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros