slow joinings of strings

Karol Bryd kbryd at losthighway._nospam_eu.org
Tue Jan 30 07:34:58 EST 2001


       Hi!

I want to read a file (0.6MB, 10000 lines) into memory, and want to do it as
fast as possible, this code does it, but is terribly slow

fp = open(file, 'r')
s = ''
while 1:
        line = fp.readline()
        if line == '': break
        s = s + line

(executing time 25 sec)

At first I thought that this is caused by readline() and lack of buffering
but after removing "s = s + line" executing time decreased to 0.7 seconds!
The question is how to join two strings in a more efficient way?

Regards.
-- 
       ------------------- Karol Bryd ------------------
   ---------------- http://int.gnu.org.pl ------------------



More information about the Python-list mailing list