[OT] Bit twiddling homework

Grant Edwards grant.b.edwards at gmail.com
Fri Jul 20 13:21:46 EDT 2018


On 2018-07-20, Chris Angelico <rosuav at gmail.com> wrote:
> 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.

My understanding was that the hypothesis was the Python benchmark
discrepency might be due to the CPU ADD operation being faster than
the OR operator.

> 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.

Definitely.  Any modern CPU capable of running Python will have single
cycle add/subtract/or/and/not/xor.  Addition is so important for so
many things that they will have jumped through the hoops required to
build a single cycle word-width adder.  And/or/not is so simple
there's just no sane way to make it take more than one cycle.

> For most data, most code, and most situations, integer addition is
> exactly as fast as integer bit shift.

Yes.

-- 
Grant Edwards               grant.b.edwards        Yow! Was my SOY LOAF left
                                  at               out in th'RAIN?  It tastes
                              gmail.com            REAL GOOD!!



More information about the Python-list mailing list