popularity of numbers (was Re: Reference counts)

Delaney, Timothy tdelaney at avaya.com
Thu May 24 20:13:40 EDT 2001


> "Helen Dawson" <helend at accessone.com> wrote in message
> news:3B0CB0B6.EA9D2EF1 at accessone.com...
>     ...
> > and 70 to 15. Zero is a popular number, but 1495? These results
> 
[snip Alex being erudite ;)]

Here's a good example showing how various integers are stored and referenced
... Pythonware 2.0, Windows 2000.

import sys

offset = 100000

for i in range(-2 - offset, 151 - offset):
    j = i + offset
    k = str(i + offset)
    print '%s\t%4d%4d' % (k, sys.getrefcount(j), sys.getrefcount(i +
offset),)

First column is the integer, second column is the refcount using an
explicitly-referenced object, third column is the refcount when using a new
reference to the object whilst still holding the old one.

All negative integers less than -1 do not intern their instances - a new
instance is created each time.

Integers in the range [-1,99] share their instances - i.e. you will get the
same instance each time, no matter how it is created.

Integers >= 100 are not interned - a new object will be created each time.

As an interesting point, note that early powers of 2 (1, 2, 4, 8, 16, 32,
64) have increased refcounts, as do some of the early powers of 2 - 1 (3, 7,
15, excluding 0 and 1 of course which we would always expect to have high
refcounts).

This also explains why there was such a notable number of references to 15
earlier in the thread ... it is 2**4-1.

-2	   2   1
-1	   3   3
0	  53  53
1	  37  37
2	  22  22
3	   7   7
4	  14  14
5	   4   4
6	   4   4
7	   5   5
8	  10  10
9	   4   4
10	   3   3
11	   5   5
12	   3   3
13	   3   3
14	   3   3
15	   5   5
16	   4   4
17	   3   3
18	   3   3
19	   3   3
20	   3   3
21	   3   3
22	   5   5
23	   5   5
24	   3   3
25	   3   3
26	   3   3
27	   3   3
28	   3   3
29	   3   3
30	   3   3
31	   3   3
32	   4   4
33	   3   3
34	   3   3
35	   3   3
36	   3   3
37	   3   3
38	   3   3
39	   3   3
40	   3   3
41	   3   3
42	   3   3
43	   3   3
44	   3   3
45	   3   3
46	   3   3
47	   3   3
48	   3   3
49	   3   3
50	   3   3
51	   3   3
52	   3   3
53	   3   3
54	   3   3
55	   3   3
56	   4   4
57	   3   3
58	   3   3
59	   3   3
60	   3   3
61	   3   3
62	   3   3
63	   3   3
64	   5   5
65	   3   3
66	   3   3
67	   3   3
68	   3   3
69	   3   3
70	   3   3
71	   3   3
72	   3   3
73	   3   3
74	   3   3
75	   3   3
76	   3   3
77	   3   3
78	   3   3
79	   3   3
80	   3   3
81	   3   3
82	   3   3
83	   3   3
84	   3   3
85	   3   3
86	   3   3
87	   3   3
88	   3   3
89	   3   3
90	   3   3
91	   3   3
92	   3   3
93	   3   3
94	   3   3
95	   3   3
96	   3   3
97	   3   3
98	   3   3
99	   3   3
100	   2   1
101	   2   1
102	   2   1
103	   2   1
104	   2   1
105	   2   1
106	   2   1
107	   2   1
108	   2   1
109	   2   1
110	   2   1
111	   2   1
112	   2   1
113	   2   1
114	   2   1
115	   2   1
116	   2   1
117	   2   1
118	   2   1
119	   2   1
120	   2   1
121	   2   1
122	   2   1
123	   2   1
124	   2   1
125	   2   1
126	   2   1
127	   2   1
128	   2   1
129	   2   1
130	   2   1
131	   2   1
132	   2   1
133	   2   1
134	   2   1
135	   2   1
136	   2   1
137	   2   1
138	   2   1
139	   2   1
140	   2   1
141	   2   1
142	   2   1
143	   2   1
144	   2   1
145	   2   1
146	   2   1
147	   2   1
148	   2   1
149	   2   1
150	   2   1

Tim Delaney




More information about the Python-list mailing list