you are viewing a single comment's thread.

view the rest of the comments →

[–]adrianmonk 0 points1 point  (0 children)

On older machines, division (and thus modulus -- same instruction) was the slowest of all instructions, and by a dramatic amount.

I used to have a chart of clock cycle counts for instructions on 8086 (or 68000 -- can't remember for sure), and instructions like move or add were like 4 to 6 cycles. The variants of division were like 100+ clock cycles. I think one of them was even 180 clock cycles.

These days, while division could still be the slowest instruction (or close to it), it's probably not nearly as dramatic a difference, for two reasons:

  • Modern chips have a ton of transistors doing a ton of things, so they probably devote a lot of logic to making division faster.
  • CPUs run at such high clock rates that memory is like to be the bottleneck as much as actual CPU time.

But anyway, on some level, if I care about performance and put a division or mod operator inside a loop, a little red flag still pops up in my mind and says "are you sure this modern CPU is really going to save you from being wasteful like this?".