Making code faster

Gerhard Häring gerhard.haering at gmx.de
Tue Jul 16 12:31:14 EDT 2002


JB wrote in comp.lang.python:
> How can I make the following code faster? I open a (text) 
> file and read the lines in the file. The lines of the file 
> have the form
> 
> [<string>,<string>]
> 
> After the lines have been loaded, I set up a new list via 
> the function tmp1.
> 
>     i = -1
>     print 'loading file'
>     def tmp1(s):
>       global i
>       i += 1
>       return QSimpleViewItem(i,self.sv.lv.newColor,eval(s))
> 
>     try:
>       myfile = open(filename)
>     except:
>       pass
>     else:
>       tmp = myfile.readlines()
>       print 'lines loaded'
>       datei.close()
>       self.sv.lv.rows = map(tmp1,tmp)
>       self.sv.lv.visible = range(len(self.sv.lv.rows))
>       print 'lines decoded',self.sv.lv.rows[0].col[0]
> 
> Any ideas? It is astonishing that loading the lines from 
> external storage takes much less time than the mapping 
> itself!

The line "self.sv.lv.rows = map(tmp1,tmp)" makes some call into an
external library (PyQt?) that's likely taking up the whole time.

Gerhard
-- 
mail:   gerhard <at> bigfoot <dot> de       registered Linux user #64239
web:    http://www.cs.fhm.edu/~ifw00065/    OpenPGP public key id AD24C930
public key fingerprint: 3FCC 8700 3012 0A9E B0C9  3667 814B 9CAA AD24 C930
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))



More information about the Python-list mailing list