[OT] Bit twiddling homework

Chris Angelico rosuav at gmail.com
Fri Jul 20 11:49:13 EDT 2018


On Sat, Jul 21, 2018 at 1:14 AM, Grant Edwards
<grant.b.edwards at gmail.com> wrote:
> On 2018-07-20, Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
>
>> While I suspect Python isn't micro-optimizing, take into account
>> that most processors do have an "increment"/"decrement" operation --
>> since that is done so much at the low-level. Also, just general
>> integer addition is common, so the hardware may be optimized for
>> doing them fast. Boolean operations may not be as well optimized.
>
> Boolean operations are also very common at the lowest level, and they
> involve far simpler logic than does addition.  I refuse to believe
> there's an extant processor in common use where an ADD is faster than
> an OR unless somebody shows me the processor spec sheet.
>

"Faster than"? I'd agree with you. But "as fast as"? I believe that's
how most modern CPUs already operate. (Well, mostly.) There are
sophisticated methods of daisy-chaining the carry bit that mean the
overall addition can be performed remarkably quickly, and the end
result is a one-clock ADD operation, same as OR. For most data, most
code, and most situations, integer addition is exactly as fast as
integer bit shift.

ChrisA


More information about the Python-list mailing list