[Python-Dev] xrange accepting non-ints

Fredrik Lundh fredrik at pythonware.com
Thu Aug 24 18:14:01 CEST 2006


Neal Norwitz wrote:

> I've profiled various combinations.  Here are the various results
> normalized doing xrange(0, 1e6, 1):
> 
> Run on all integer (32-bit) values for start, step, end:
> C xrange and iter:  1
> Py xrange w/C iter: 1

in real life, loops are a lot shorter than that.

if you take that into account, you don't even have to run the benchmark 
to realize that calling a Python function and checking the arguments 
before calling a C function takes more time than calling a C function.

even if you skip the "check the arguments" part, you take a 5% hit:

 > timeit -s"def myxrange(a,xrange=xrange): return xrange
   (a)" "for i in myxrange(100): pass"
100000 loops, best of 3: 5.28 usec per loop

 > timeit "for i in xrange(100): pass"
100000 loops, best of 3: 4.98 usec per loop

 > timeit -s"def myxrange(a,b=None,c=None,xrange=xrange):
   return xrange(a,b,c)" "for i in myxrange(0,100,1): pass"
100000 loops, best of 3: 5.58 usec per loop

 > timeit "for i in xrange(0,100,1): pass"
100000 loops, best of 3: 5.27 usec per loop

I doubt adding more code to the myxrange function will speed it up...

</F>



More information about the Python-Dev mailing list