Why it is so dramatical?

Bo M. Maryniuck b.maryniuk at forbis.lt
Mon Sep 2 05:07:25 EDT 2002


The task:
	Adding a text to a text.

The problem:
	It is tragically slow to add string to huge string.

The source:
-----------------8<---------------------------
import time

_body = ''
_data = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'

START_TIME = time.time()
for a in xrange(0, 0xfff):
    _body += _data
print "'+=' operator:", time.time() - START_TIME, "sek."

_body = []
START_TIME = time.time()
for a in xrange(0, 0xfff):
    _body.append(_data)
print "append() method:", time.time() - START_TIME, "sek."
-----------------8<---------------------------

The usage result:
-----------------8<---------------------------
*bo at oberon:(~/work/spike/benchmarks) python str_vs_list.py 
'+=' operator: 6.96335601807 sek.
append() method: 0.0111969709396 sek.
-----------------8<---------------------------

The question: Why?

The other question: 
	Is it true, that I should through all the Python life 
	use lists and then join it into single text instead to use += operator?

-- 
Regards, Bogdan

It is a mess, pure and simple.  Sucks to be away from Unix, huh?
		-- man perlfaq3





More information about the Python-list mailing list