[Python-Dev] sum(...) limitation

Paul Moore p.f.moore at gmail.com
Sat Aug 9 10:36:31 CEST 2014


On 9 August 2014 06:08, Steven D'Aprano <steve at pearwood.info> wrote:
> py> with Stopwatch():
> ...     sum(carray)  # carray is a numpy array of 75000000 floats.
> ...
> 112500000.0
> time taken: 52.659770 seconds
> py> with Stopwatch():
> ...     numpy.sum(carray)
> ...
> 112500000.0
> time taken: 0.161263 seconds
>
>
>>  Why have builtin sum at all if its use comes with so many caveats?
>
> Because sum() is a perfectly reasonable general purpose tool for adding
> up small amounts of numbers where high floating point precision is not
> required. It has been included as a built-in because Python comes with
> "batteries included", and a basic function for adding up a few numbers
> is an obvious, simple battery. But serious programmers should be
> comfortable with the idea that you use the right tool for the right job.

Changing the subject a little, but the Stopwatch function you used up
there is "an obvious, simple battery" for timing a chunk of code at
the interactive prompt. I'm amazed there's nothing like it in the
timeit module...

Paul


More information about the Python-Dev mailing list