[New-bugs-announce] [issue15503] concatenating string in dict unexpected performance

Petri Heinilä report at bugs.python.org
Mon Jul 30 16:20:01 CEST 2012


New submission from Petri Heinilä:

In concatenating string in dict somehow radically depends the added string size.

code 

----
import timeit

s1 = """
text = ""
for i in range(1,50000):
  text += "12345678901234567890"
"""
print("str cat 20: {0}s".format(timeit.timeit(s1,number=1)))

s2 = """
d = dict()
d["text"] = ""
for i in range(1,50000):
  d["text"] += "12345"
"""
print("dict cat 5: {0}s".format(timeit.timeit(s2,number=1)))

s3 = """
d = dict()
d["text"] = ""
for i in range(1,50000):
  d["text"] += "1234567890"
"""
print("dict cat 10: {0}s".format(timeit.timeit(s3,number=1)))


s4 = """
d = dict()
d["text"] = ""
for i in range(1,50000):
  d["text"] += "12345678901234567890"
"""
print("dict cat 20: {0}s".format(timeit.timeit(s4,number=1)))

----

gives:

str cat 20: 0.011670112609863281s
dict cat 5: 2.994051933288574s
dict cat 10: 6.61974310874939s
dict cat 20: 34.230541944503784s

----------
assignee: collinwinter
components: Benchmarks
files: string_cat.py
messages: 166901
nosy: collinwinter, hevi
priority: normal
severity: normal
status: open
title: concatenating string in dict unexpected performance
type: performance
versions: Python 3.2
Added file: http://bugs.python.org/file26603/string_cat.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue15503>
_______________________________________


More information about the New-bugs-announce mailing list