Python speed-up

Thomas Guettler guettli at thomas-guettler.de
Wed Sep 22 10:36:17 EDT 2004


Am Wed, 22 Sep 2004 16:06:04 +0200 schrieb Guyon Morée:

> Hi all,
> 
> I am working on a Huffman encoding exercise, but it is kinda slow. This is
> not a big problem, I do this to educate myself :)
> 
> So I started profiling the code and the slowdown was actually taking place
> at places where I didn't expect it.
> 
> after I have created a lookup-table-dictionary with encodings like
> {'d':'0110', 'e':'01' etc} to encode the original text like this:


> 
> for c in original_text:
>     encoded_text += table[c]

Hi Guyon,

this is slow. Try this:

e_t=[]
for c in original_text:
    e_t.append(table[c])
e_t=''.join(e_t)


Your solutions creates a new string for every +=.

HTH,
 Thomas





More information about the Python-list mailing list