Math optimization

jepler at unpythonic.net jepler at unpythonic.net
Sat Sep 21 18:05:08 EDT 2002


Did you bother benchmarking?  I'd be surprised to see a huge difference,
since the overhead of the bytecode loop should be much much greater than
the cost of a single division operation.

Except for 'if 0' and a special variable (__debug__?) which can be 0, there
is no constant folding in the compilation step.  You could fake this by
using
    def f(a, factor=3*5/7/2):
	return a/factor
instead of 
    def f(factor):
	return factor/(3*5/7/2)
(the value of default arguments is computed once, when the function is
created)

Jeff




More information about the Python-list mailing list