subtle error slows code by 10x (builtin sum()) - replace builtin sum without using import?

bdb112 boyd.blackwell at gmail.com
Fri Jul 1 22:17:45 EDT 2011


First a trap for new players, then a question to developers

Code accelerated by numpy can be slowed down by a large factor is you
neglect to import numpy.sum .

from timeit import Timer
frag = 'x=sum(linspace(0,1,1000))'
Timer(frag ,setup='from numpy import linspace').timeit(1000)
# 0.6 sec
Timer(frag, setup='from numpy import sum, linspace').timeit(1000)  #
difference is I import numpy.sum
# 0.04 sec  15x faster!

This is obvious of course - but it is very easy to forget to import
numpy.sum and pay the price in execution.

Question:
Can I replace the builtin sum function globally for test purposes so
that my large set of codes uses the replacement?

The replacement would simply issue warnings.warn() if it detected an
ndarray argument, then call the original sum
I could then find the offending code and use the appropriate import to
get numpy.sum



More information about the Python-list mailing list