[Python-ideas] Fast sum() for non-numbers
Steven D'Aprano
steve at pearwood.info
Tue Jul 16 09:31:29 CEST 2013
On Tue, Jul 16, 2013 at 08:36:05AM +0300, Sergey wrote:
> You also said that I can't make tuples __add__ faster without patching
> sum() so explained how you can do that (you never answered whether
> you like such a patch and whether you would agree to write it).
> I even wrote a simple fasttuple proof of concept [1].
> [1] http://bugs.python.org/file30917/fasttuple.py
I do not like that implementation, because it shares the underlying
storage. This means that tuples which ought to be small will grow and
grow and grow just because you have called __add__ on a different tuple.
Using Python 2.7 and your implementation above:
py> a = ft([]) # empty tuple
py> len(a._store)
0
py> b = ft([1])
py> c = a + b
py> d = ft([2]*10000)
py> c = c + d
py> len(a._store)
10001
So adding a big tuple to c changes the internal storage of a.
--
Steven
More information about the Python-ideas
mailing list