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

2014年8月07日 16:03:41 -0700

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~
_______________________________________________
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to