Does Python allow access to some of the implementation details?

Diez B. Roggisch deets at
Fri Jan 6 09:32:04 EST 2006

> The test code below shows, that extracting bits from an integer value n
> is faster when using n&0x01 than when using n%2 and I suppose it is
> because %2 tries to handle the entire integer, where &0x01 processes
> only the last two bytes of it (I come to this because the speed
> difference between &0x01 and %2 operations depends on how large the
> value n is)

I doubt that the reason is in & handling less data. The reason is that % is
effectively a division, whereas & is a logical operation. Which have always
been _way_ faster than divisions.

Of course you are right that the bitfiddling _could_ be optimized when one
knows that only certain bytes of a number would be of interest. But I
seriously doubt that python does any optimization here. However, I don't
know for sure.



More information about the Python-list mailing list