[Tutor] creating a range above & below a given number

Kent Johnson kent37 at tds.net
Mon Jun 8 04:56:22 CEST 2009


On Sun, Jun 7, 2009 at 3:34 PM, Emile van Sebille<emile at fenx.com> wrote:

> To explain why we've tended to suggest using int and minval<val<maxval as
> tests and avoid the range inclusion test, consider the following timeit
> results:

The performance penalty for 'in range(...)' is even greater when the
value is not found, because the entire list must be searched:

kent $ python -m timeit "0 <= 6000 <= 1000"
10000000 loops, best of 3: 0.111 usec per loop

kent $ python -m timeit "6 in range(100)"
100000 loops, best of 3: 1.89 usec per loop

kent $ python -m timeit "6000 in range(100)"
100000 loops, best of 3: 5.55 usec per loop

kent $ python -m timeit "6 in range(1000)"
100000 loops, best of 3: 19 usec per loop

kent $ python -m timeit "6000 in range(1000)"
10000 loops, best of 3: 55.4 usec per loop

Kent


More information about the Tutor mailing list