r/Python • • Aug 19 '17

IPython magic to profile and view your Python code as a heat map

https://github.com/csurfer/pyheatmagic
277 Upvotes

35 comments sorted by

View all comments

Show parent comments

3

u/takluyver IPython, Py3, etc Aug 19 '17

Which, for an integer, is equivalent to dividing it by two and rounding down - b //= 2 in Python. The bit-shift operation may be more efficient, though I'd guess that it doesn't make much difference for Python code.

9

u/[deleted] Aug 19 '17

If only we had a profiler to find out.

1

u/Mattho Aug 19 '17

What would the bitshift do on python's big integers?

2

u/HannasAnarion Aug 19 '17

Same thing. They're encoded in the same way, just in 64 bits instead of 32. It should only stop working on floats.

1

u/Mattho Aug 19 '17

I didn't mean a regular int, rather what python denotes by L. It's arbitrary-sized integer, big int I think they call it. I just wonder how it works internally.

2

u/HannasAnarion Aug 19 '17

Yeah, I know what you mean. A regular python int is 32 bits. A long int is 64 bits, using the same encoding, two's complement signed integers. So the shift operations work just the same

As opposed to floating point numbers, which are a bit complicated and depend on the interpreter, but are IEEE single or double precision floating point numbers (sign bit, 8/16 exponent bits, 23/47 numeric bits). You can't use shift operations on a float.

3

u/Mattho Aug 19 '17

Yeah, I know what you mean. A regular python int is 32 bits. A long int is 64 bits, using the same encoding, two's complement signed integers.

I don't think you do. For example

foo = 34494846812312438467612454354668434566231212345543359498321343215438547847895543814889640867

is a valid statement in python, foo being of type long. It supports all (I believe) operations that proper integer does. And is nowhere near 64-bits long.

3

u/HannasAnarion Aug 19 '17

Ooooohhhh I dun goofed. I didn't know python supported arbitrary precision without importing libraries for it. After a little reading, it seems they are implemented as arrays of ints, so I would guess that the same operations are supported.

1

u/[deleted] Aug 20 '17

Python has "arbitrary precision" so it won't overflow. It will just allocate more memory for the integer.

1

u/[deleted] Aug 20 '17

1

u/Mattho Aug 20 '17

I guess you forgot to point to a specific line?

1

u/[deleted] Aug 20 '17

I guess you forgot this was a free service? 😁

But "cmd+f right shift" is a thing. I am sure someone of your ability can sort it out.