[Python-ideas] Expansion of the range of small integers

Serhiy Storchaka storchaka at gmail.com
Mon Sep 17 14:41:23 CEST 2012


Now in the CPython small integer numbers from -5 up to 256 inclusive are 
preallocated at the start. It allows to reduce memory consumption and 
time of creation of the integers in this range. In particular this 
affects the speed of short enumerations. Increasing the range to the 
maximum (from -32767 to 32767 inclusive), we can speed up longer 
enumerations.

Microbenchmarks:
./python -m timeit  "for i in range(10000): pass"
./python -m timeit  -s "a=[0]*10000"  "for i, x in enumerate(a): pass"
./python -m timeit  -s "a=[0]*10000"  "i=0"  "for x in a: i+=1"
./python -m timeit  -s "a=[0]*10000"  "for i in range(len(a)): x=a[i]"

Results:
  non-patched     patched
    530 usec      337 usec    57%
   1.06 msec      811 usec    31%
   1.34 msec     1.13 msec    19%
   1.42 msec     1.22 msec    16%

Shortcomings:

1) Memory consumption increases by constant 1-1.5 MB. Or half of it if 
the range is expanded only in a positive direction. This is not a 
problem on most modern computers. But would be better if the parameters 
NSMALLPOSINTS and NSMALLNEGINTS have been configurable at build time.

2) A little bit larger Python start time. I was not able to measure the 
difference, it is too small.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: larger_small_ints.diff
Type: text/x-patch
Size: 1981 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120917/39f3052d/attachment.bin>


More information about the Python-ideas mailing list