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

Ethan Furman ethan at stoneleaf.us
Fri Aug 8 01:01:49 CEST 2014


On 08/07/2014 03:06 PM, Chris Barker wrote:

[snip timings, etc.]

I don't remember where, but I believe that cPython has an optimization built in for repeated string concatenation, which is probably why you aren't seeing big differences between the + and the sum().

A little testing shows how to defeat that optimization:

blah = '' for string in ['booyah'] * 100000: blah = string + blah

Note the reversed order of the addition.

--> timeit.Timer("for string in ['booya'] * 100000: blah = blah + string", "blah = ''").repeat(3, 1) [0.021117210388183594, 0.013692855834960938, 0.00768280029296875]

--> timeit.Timer("for string in ['booya'] * 100000: blah = string + blah", "blah = ''").repeat(3, 1) [15.301048994064331, 15.343288898468018, 15.268463850021362]

-- Ethan



More information about the Python-Dev mailing list