Python's layout of low-value ints in memory -
My question is, where are these patterns (below) started?
I learned (somewhere) that there are unique "copies" in Python, if it is the correct word, then for the small integer for example:
& gt; & Gt; & Gt; X = y = 0> gt; & Gt; & Gt; ID (0) 4297074752 & gt; & Gt; & Gt; ID (x) 42 9 7074752 & gt; & Gt; & Gt; ID (Y) 42 9 7074752 & gt; & Gt; & Gt; X + = 1> and gt; & Gt; ID (x) 42 9 7074728 & gt; & Gt; & Gt; Y 0
When I look at the memory places ints, there is a simple pattern quickly:
& gt; & Gt; & Gt; N = ID (0)> & gt; & Gt; & Gt; For category (5): ... print i, n - id (i) ... 0 1 24 2 48 3 72 4 96 >>; & Gt; Bin (24) '0b11000'
It is not clear to me why it is chosen as salary increment. Apart from this, I can not explain this method to everyone above 256:
gt; & Gt; Prev = 0> gt; & Gt; & Gt; For the category (270) I: ... t = (id (i-1), id (i)) ... diff = t [0] - t [1] ... if diff! = Prev:. .. print I-1, i, t, diff ... prev = diff ... -1 0 (42 9 7074776, 42 9 7074752) 24 35 36 (42 9 7073912, 42 9 7075864) -1952 36 37 ( 42 9 7075864, 42 9 7075840) 24 76 77 4297074904, 4297076856) -1952 77 78 (4297076856, 4297076832) 24 117 118 (4297075896, 4297077848) -1952 118 119 (4297077848, 4297077824) 24 158 159 (4297076888, 4297078840) - 1952 159 160 (4297078840, 4297078816) 24 199 200 (42 9 7077880, 42 9 7079832) -1952 200 201 (42 9 7079832, 42 9 7079808) 24 240 241 (42 9 7078872, 42 9 7080824) -1952 241 242 ( 42 9 7080824, 42 7080800 9) 24 256 257 (42 9 7080464, 42 9 7155264) -74800 257 258 (42 9 7155072, 42 9 7155288) -216 25 9 260 (42 9 7155072, 42 9 7155336) -264 260 261 (42 9 7155048, 42 9 7155432) -384 261 262 (42 9 7155024, 42 9 7155456) -432 262 263 (42 9 7380280, 42 9 7155384) 224896 263 264 (42 9 7155000 42 9 7155240) -240 264 265 (42 9 7155072, 42 9 7155216)) -144 266 267 (42 9 7155072, 42 9 7155168) -96 267 268 (42 9 715
update:
Any ideas, clues, place to see?
Edit: And what is special about 24?
Update:
sys.getsizeof ()
in the standard that returns24
when I argue it as1
I'm pretty much bytes, but on 64-bit machines, we have 8 bytes for each type, value and ref count, besides, see, and C API reference.Spend some time with "source" in the link from Peter Hansen in the comment. An int (
* has no definition of int_int
beyond the announcement), but I found:#define Define NSMALLPOSINTS 257 # NSMALLNEGINTS 5
Low-value integer is undone, high value integers are assigned to them when they Let's count. The intersections of the source code are the same objects on my system,
& gt; & Gt; & Gt; ID (2) == ID (1 + 1) True & gt; & Gt; & Gt; ID (1000) == ID (1000 + 0) false & gt; & Gt; & Gt; ID (1000) == ID (1000) is true
You will also see that the ID depends on the system. Are they memory addresses, which are set by system allocator (or possibly linker, for static objects)?
& gt; & Gt; & Gt; Edit ID: (0) 8402324
Edit: The reason id (1000) is == id (1000)
because the Python compiler notice that it is static in two integers The collection code is the same, so it only allocates an object to both. This would be an unacceptable performance hit on runtime, but at compile time this is not noticeable. (Yes, interpreter is also a compiler. Most interpreters are also compilers, not very few.)
Comments
Post a Comment