Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #20799

Re: sum() requires number, not simply __add__

From Duncan Booth <duncan.booth@invalid.invalid>
Newsgroups comp.lang.python
Subject Re: sum() requires number, not simply __add__
Date 2012-02-24 11:40 +0000
Message-ID <XnsA00376AD2D21Cduncanbooth@127.0.0.1> (permalink)
References <91c71e41-b98c-46f6-b3af-bed894cf9d92@kh11g2000pbb.googlegroups.com> <CAMZYqRSYCy9vRzf=umdiiCO=qf1UqKYYMO4EmTwp47oh1oMKyQ@mail.gmail.com> <mailman.91.1330033355.3037.python-list@python.org>

Show all headers | View raw


Stefan Behnel <stefan_ml@behnel.de> wrote:

> I know that you just meant this as an example, but it's worth
> mentioning in this context that it's not exactly efficient to "sum up"
> lists this way because there is a lot of copying involved. Each adding
> of two lists creates a third one and copies all elements into it. So
> it eats a lot of time and space.

If you search back through this group far enough you can find an 
alternative implementation of sum that I suggested which doesn't have the 
same performance problem with lists or strings and also improves the 
accuracy of the result with floats.

In effect what it does is instead of:
  (((((a + b) + c) + d) + e) + f)
it calculates the sum as:
  ((a + b) + (c + d)) + (e + f)

i.e. in as balanced a manner as it can given that it still has to work from 
left to right.

Of course that could still change the final result for some user defined 
types and never having converted my code to C I have no idea whether or not 
the performance for the intended case would be competitive with the builtin 
sum though I don't see why it wouldn't be.

-- 
Duncan Booth http://kupuguy.blogspot.com

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

sum() requires number, not simply __add__ Buck Golemon <buck@yelp.com> - 2012-02-23 13:19 -0800
  Re: sum() requires number, not simply __add__ Buck Golemon <buck@yelp.com> - 2012-02-23 13:23 -0800
    Re: sum() requires number, not simply __add__ Arnaud Delobelle <arnodel@gmail.com> - 2012-02-23 21:41 +0000
    Re: sum() requires number, not simply __add__ Chris Angelico <rosuav@gmail.com> - 2012-02-24 08:53 +1100
      Re: sum() requires number, not simply __add__ Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-23 23:33 +0000
        Re: sum() requires number, not simply __add__ Chris Angelico <rosuav@gmail.com> - 2012-02-24 10:39 +1100
        Re: sum() requires number, not simply __add__ Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2012-02-24 12:41 +0100
          Re: sum() requires number, not simply __add__ Roy Smith <roy@panix.com> - 2012-02-24 08:23 -0500
            Re: sum() requires number, not simply __add__ Terry Reedy <tjreedy@udel.edu> - 2012-02-24 16:58 -0500
    Re: sum() requires number, not simply __add__ Arnaud Delobelle <arnodel@gmail.com> - 2012-02-23 21:59 +0000
    Re: sum() requires number, not simply __add__ Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-23 15:00 -0700
    Re: sum() requires number, not simply __add__ Chris Angelico <rosuav@gmail.com> - 2012-02-24 09:04 +1100
    Re: sum() requires number, not simply __add__ Arnaud Delobelle <arnodel@gmail.com> - 2012-02-23 22:09 +0000
  Re: sum() requires number, not simply __add__ Arnaud Delobelle <arnodel@gmail.com> - 2012-02-23 21:32 +0000
  Re: sum() requires number, not simply __add__ Chris Rebert <clp2@rebertia.com> - 2012-02-23 13:32 -0800
    Re: sum() requires number, not simply __add__ Buck Golemon <buck@yelp.com> - 2012-02-23 13:38 -0800
      Re: sum() requires number, not simply __add__ Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-23 14:54 -0700
  Re: sum() requires number, not simply __add__ Stefan Behnel <stefan_ml@behnel.de> - 2012-02-23 22:42 +0100
    Re: sum() requires number, not simply __add__ Duncan Booth <duncan.booth@invalid.invalid> - 2012-02-24 11:40 +0000
  Re: sum() requires number, not simply __add__ Peter Otten <__peter__@web.de> - 2012-02-24 09:29 +0100

csiph-web