slow joinings of strings

Remco Gerlich scarblac at pino.selwerd.nl
Tue Jan 30 08:05:03 EST 2001


Karol Bryd <kbryd at losthighway._nospam_eu.org> wrote in comp.lang.python:
> 
>        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?

Why do you read them in line by line in the first place?

s = open(file, 'r').read()

"s = s+line" has to create a nice string every time, and copy the other two
into it, one of them nearly 0.6M at the last line. That will be slow.

-- 
Remco Gerlich



More information about the Python-list mailing list