[XML-SIG] New data on speed of string appending
Jeremy Hylton
jeremy@beopen.com
Thu, 24 Aug 2000 12:36:14 -0400 (EDT)
>>>>> "TP" == tpassin <tpassin@home.com> writes:
TP> Remember back in June (June 22,2000), Bjorn Pettersen showed how
TP> using StringIO instead of just appending to a string was
TP> **much** faster? Then others showed that using
TP> list.append/join(list) was also very fast.
[...]
TP> The results are dramatic. Method 1) is as good as or better
TP> than anything until the string length exceeds about 1000 bytes.
TP> Then Method 1 starts slowing down. Above about 4000 bytes, it's
TP> really getting ssslllooowww. Here is a table of the results on
TP> my system - 450 MHz PIII running Win98, Python 1.5.2.
This is an empirical confirmation of something that analysis shows
clearly. The repeated string concatenation method (buf = buf + s)
creates a new string object each time; this costs a malloc and two
memcpys. As the string increases, the begin of the string is copied
repeatedly. The other methods defer the creation of a new string
object until it is needed; it defers the mallocs and memcpy until the
end and does them once.
Jeremy