question about xrange performance
_wolf
wolfgang.lipp at gmail.com
Fri Apr 17 14:39:00 EDT 2009
lately i realized a slow running portion of my application, and a
quick profiling nourished the suspicion that, of all things, calls to
`xrange().__contains__` (`x in b` where `b = xrange(L,H)`) is the
culprit. to avoid any other influences, i wrote this test script with
class `xxrange` being a poor man’s `xrange` replacement:
########################################################
class xxrange( object ):
def __init__( self, start, stop ):
self.start = start
self.stop = stop
def __contains__( self, x ):
return ( x == int( x ) ) and self.start <= x < self.stop
import cProfile
from random import randint
test_integers = [ randint 0, 5000 ) for i in xrange( 8000 ) ]
test_range_a = xxrange( 10000, 20000 )
test_range_b = xrange( 10000, 20000 )
def a():
print test_range_a.__class__.__name__
for x in test_integers:
x in test_range_a
def b():
print test_range_b.__class__.__name__
for x in test_integers:
x in test_range_b
cProfile.run('a()')
cProfile.run('b()')
########################################################
now this is the output, surprise:
########################################################
xxrange
8003 function calls in 0.026 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno
(function)
1 0.000 0.000 0.026 0.026 <string>:1(<module>)
1 0.012 0.012 0.026 0.026 xrange-profiler.py:18(a)
8000 0.014 0.000 0.014 0.000 xrange-profiler.py:9
(__contains__)
1 0.000 0.000 0.000 0.000 {method 'disable' of
'_lsprof.Profiler' objects}
xrange
3 function calls in 4.675 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno
(function)
1 0.000 0.000 4.675 4.675 <string>:1(<module>)
1 4.675 4.675 4.675 4.675 xrange-profiler.py:23(b)
1 0.000 0.000 0.000 0.000 {method 'disable' of
'_lsprof.Profiler' objects}
########################################################
can it be that a simple diy-class outperforms a python built-in by a
factor of 180? is there something i have done the wrong way?
omissions, oversights? do other people get similar figures?
cheers
More information about the Python-list
mailing list