$> 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
$> 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
$> 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 xrange(1,10000001):
... try:
... b=a
... except:
... pass
... tout=time.time()
... print tout-tin
...
>>> tryit()
0.629939079285
>>> tryit()
0.681260108948
>>> tryit()
0.61185503006
>>> tryit()
0.590117931366
>>> tryit()
0.5790579319
>>> tryit()
0.570842981339
>>> tryit()
0.597903966904
$> cat tryit.py
import time
tin=time.time()
a="97757589545391667"
for x in xrange(1,10000001):
try:
b=a
except:
pass
tout=time.time()
print tout-tin
$> python tryit.py
1.59175992012
$> python tryit.py
1.33616805077
$> python tryit.py
1.46257305145
$> python tryit.py
1.36383104324
$> python tryit.py
1.40599513054
ASKER
php -r '$a="5";$totaltime=0;$totalcount=0;for ($z=0;$z<100;$z++) { $totalcount++; $timein=microtime(true); for ($x=0;$x<1000000;$x++) { <<<command>>> } $timeout=microtime(true); $totaltime+=($timeout-$timein); } echo "count:$totalcount, time:$totaltime, avg:",$totaltime/$totalcount;'
The Python code, run from console by calling tryit():def tryit():
import time
a="5"
totaltime=0
totalcount=0
for z in xrange(0,100):
totalcount=totalcount+1
timein=time.time()
for x in xrange(0,1000000):
<<<command>>>
timeout=time.time()
totaltime=totaltime+(timeout-timein)
print "count:{s}, time:{t}, avg:{u}".format(s=totalcount,t=totaltime,u=totaltime/totalcount)
The test cases are loop iteration, simple assignment, assignment with type conversion, and (for python only) assignment with type conversion using try/except. In each test case, "<<<command>>>" was replaced with the appropriate command for the test. For the exception test case, the command text was:try:
b=int(a)
except:
pass
The results as average duration in seconds of a single 1000000-iteration loop:a as integer 0.05742 0.06275 0.03783 0.04245
When taking a substring:a="abcdefg" 0.33876 0.38429 0.10930 0.11866
For PHP, the command was "$b=substr($a,3,3)". For Python, "b=a[3,6]". PHP's dismal performance highlights the difference between a PHP function call and a Python slice. Again, not unexpected, and one of the things I really like about Python. ASKER
ASKER
PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.
TRUSTED BY
Open in new window
and to run the code not in the interactive mode. (I did not test but it could slow down the execution.)Also, integer type may be implemented differently in Python as the int type has almost no practical limit in the size of the number. I do not know how the Perl int is implemented.