Bitshifts and "And" vs Floor-division and Modular

rusi rustompmody at gmail.com
Thu Sep 6 23:38:16 EDT 2012


On Sep 7, 5:01 am, jimbo1qaz <jimmyli1... at gmail.com> wrote:
> Is it faster to use bitshifts or floor division? And which is better, & or %?
> All divisors and mods are power of 2, so are binary operations faster? And are they considered bad style?

On an 8086/8088 a MUL (multiply) instruction was of the order of 100
clocks and a DIV nearly 200 compared to ADD, OR etc which were
something like 8 (IIRC -- this is decades-stale knowledge)
On most modern processors (after the pentium) the difference has
mostly vanished.  I cant find a good data sheet to quote though -- one
of the sad things about modern processors is that the clocks which
were politely offered by intel earlier have now stopped presumably
because cache-(in)coherence, pipelining etc are more likely to
dominate the number of clocks than the specific instruction.

This question is interesting to a programmer but meaningless at the
python level (as others have pointed out).  If it still interests you,
work at the C (or still better assembly) level and use a more
finegrained timer measure -- the finest being the RDTSC instruction.



More information about the Python-list mailing list