[Python-Dev] Silly little benchmark
Tim Peters
tim@digicool.com
Tue, 10 Jul 2001 17:26:13 -0400
Here are results on Win2K. In part it just confirms that
xrange(large_number) is a poor way to drive benchmarks (the overhead of
creating and destroying gagilliblobs of unused integers is no help; OTOH,
2.0 certainly appears to be speedier at creating and destroying gagilliblobs
of useless integers! a common cause for slowdowns of that nature is
ill-considered "special case" optimizations that turn out to cost more than
they save, although I have no particular reason to suspect that here).
Note that Windows Python has an excellent clock() function (it's real time,
not user time, and has better than microsecond resolution).
File skip.py:
N = 100000
TRIPS = 3
if 0:
indices = xrange(N) # common but ill-advised
else:
indices = [None] * N # better
def t1():
for i in indices: pass
def t2():
for i in indices: x = 1
def t3():
for i in indices: x = ``1`+`2``
def timeit(f):
from time import clock
start = clock()
f()
finish = clock()
return finish - start
for f, tag in (t1, "pass"), (t2, "x=1"), (t3, "x=``1`+`2``"):
print "%-12s" % tag,
# Warm up.
f(); f(); f()
for i in range(TRIPS):
elapsed = timeit(f)
print "%6.3f" % elapsed,
print
"""
Results:
With
indices = xrange(N)
C:\Code>\Python20\python.exe skip.py
pass 0.038 0.038 0.039
x=1 0.049 0.049 0.049
x=``1`+`2`` 0.421 0.420 0.421
C:\Code>\Python21\python.exe skip.py
pass 0.042 0.042 0.042
x=1 0.053 0.053 0.053
x=``1`+`2`` 0.456 0.456 0.455
C:\Code>python\dist\src\PCbuild\python skip.py # CVS
pass 0.040 0.039 0.039
x=1 0.050 0.051 0.050
x=``1`+`2`` 0.449 0.452 0.452
With
indices = [None] * N
instead:
C:\Code>\Python20\python.exe skip.py
pass 0.035 0.034 0.034
x=1 0.046 0.046 0.046
x=``1`+`2`` 0.414 0.413 0.413
C:\Code>\Python21\python.exe skip.py
pass 0.037 0.037 0.037
x=1 0.048 0.048 0.048
x=``1`+`2`` 0.451 0.448 0.453
C:\Code>python\dist\src\PCbuild\python skip.py # CVS
pass 0.031 0.030 0.031
x=1 0.041 0.042 0.041
x=``1`+`2`` 0.438 0.447 0.444
"""