[Python-Dev] Long Multiplication is not commutative.

Guido van Rossum guido@python.org
Thu, 06 Apr 2000 17:32:36 -0400


> This buddy...
> 
> >>> def ifact3(n) :
> ...  p = 1L
> ...  for i in range(1,n+1) :
> ...    p = i*p
> ...  return p
> 
> performs better by a factor of 1.8 than this one:
> 
> >>> def ifact1(n) :
> ...  p = 1L
> ...  for i in range(1,n+1) :
> ...    p = p*i
> ...  return p
> 
> The analysis of this behavior is quite simple if you look at the
> implementation of long_mult. If the left operand is big and the
> right is small, there are much more carry operations performed,
> together with more loop overhead.
> Swapping the multiplicands would be a 5 line patch.
> Should I submit it?

Yes, go for it.  I would appreciate a bunch of new test cases that
exercise the new path through the code, too...

--Guido van Rossum (home page: http://www.python.org/~guido/)