[Python-Dev] sum(...) limitation
Chris Barker
chris.barker at noaa.gov
Mon Aug 4 18:25:12 CEST 2014
On Sat, Aug 2, 2014 at 1:35 PM, David Wilson <dw+python-dev at hmmz.org> wrote:
> > Repeated list and str concatenation both have quadratic O(N**2)
> > performance, but people frequently build up strings with +
>
> join() isn't preferable in cases where it damages readability while
> simultaneously providing zero or negative performance benefit, such as
> when concatenating a few short strings, e.g. while adding a prefix to a
> filename.
>
Good point -- I was trying to make the point about .join() vs + for strings
in an intro python class last year, and made the mistake of having the
students test the performance.
You need to concatenate a LOT of strings to see any difference at all -- I
know that O() of algorithms is unavoidable, but between efficient python
optimizations and a an apparently good memory allocator, it's really a
practical non-issue.
> Although it's true that join() is automatically the safer option, and
> especially when dealing with user supplied data, the net harm caused by
> teaching rote and ceremony seems far less desirable compared to fixing a
> trivial slowdown in a script, if that slowdown ever became apparent.
>
and it rarely would.
Blocking sum( some_strings) because it _might_ have poor performance seems
awfully pedantic.
As a long-time numpy user, I think sum(a_long_list_of_numbers) has
pathetically bad performance, but I wouldn't block it!
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20140804/0f1621d3/attachment.html>
More information about the Python-Dev
mailing list