Multiplication optimization

Atanas Banov enterr at gmail.com
Sun Feb 19 03:29:30 EST 2006


Paul McGuire wrote:
> Does Python's run-time do any optimization of multiplication
> operations, like it does for boolean short-cutting?  That is, for a
> product a*b, is there any shortcutting of (potentially expensive)
> multiplication operations

no. and the reason is very simple: to the extent such optimization
makes sense, it has been done on assembler/CPU level already. i.e. when
the multiplication is mapped to the machine code
   IMUL  <op>
the Pentium processor would be smart enough not to do the work if AX or
the op are 0 or 1.

Python has no job trying to outsmart Intel (and the other chipmakers).
Boolean shortcuts are useful for entirely different reason, not speed.
e.g.
        if lastDigit == 0 or i % lastDigit != 0:
            break

if both operands of OR were to be evaluated, that would end up with
division-by-zero exception for lastDigit=0.




More information about the Python-list mailing list