Author scoder
Recipients scoder
Date 2011-02-03.19:25:20
SpamBayes Score 5.28094e-05
Marked as misclassified No
Message-id <1296761121.69.0.66743926851.issue11107@psf.upfronthosting.co.za>
In-reply-to
Content
Follow-up to ticket 10227. The following facts seem to indicate that it would be worth caching constant instances of the slice type, such as in [:] or [:-1].

with cached slice instance:

$ ./python -m timeit -s 'l = list(range(100)); s=slice(None)' 'l[s]'
1000000 loops, best of 3: 0.464 usec per loop
$ ./python -m timeit -s 'l = list(range(10)); s=slice(None)' 'l[s]'
10000000 loops, best of 3: 0.149 usec per loop
$ ./python -m timeit -s 'l = list(range(10)); s=slice(None,1)' 'l[s]'
10000000 loops, best of 3: 0.135 usec per loop

uncached normal usage:

$ ./python -m timeit -s 'l = list(range(100))' 'l[:]'
1000000 loops, best of 3: 0.499 usec per loop
$ ./python -m timeit -s 'l = list(range(100))' 'l[:1]'
10000000 loops, best of 3: 0.171 usec per loop

Timings based on Python 3.2 rc2.

A quick grep against the py3k stdlib finds 2096 lines in 393 files that use constant slices.
History
Date User Action Args
2011-02-03 19:25:21scodersetrecipients: + scoder
2011-02-03 19:25:21scodersetmessageid: <1296761121.69.0.66743926851.issue11107@psf.upfronthosting.co.za>
2011-02-03 19:25:20scoderlinkissue11107 messages
2011-02-03 19:25:20scodercreate