Making code faster

Bengt Richter bokr at oz.net
Wed Jul 17 03:42:19 EDT 2002


On Tue, 16 Jul 2002 18:20:23 +0200, JB <jblazi at hotmail.com> wrote:

>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>]
- Do you mean that literally? I.e., would
    "['abc','def']\n"
  be a valid example?
- Why do you have to eval() it? That is  probably timeconsuming compared to alternatives.
- Were the lines created by repr(someList)?
- Are all the <string>s actually quoted character sequences, or
  are there numbers too?
- Are there always two elements in the list?
>
>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))
I can't tell what the enclosing context is that defines 'self',
but you are repeating a lot of lookup doing self.sv.lv.newColor
on every call instead if doing it once at def time.
Likewise the global counter is not as fast as a local one.

To look up once, you could use a default arg, e.g.,

     # untested!
     def tmp1(s, color=self.sv.lv.newColor):
       global i
       i += 1
       return QSimpleViewItem(i,color,eval(s))

To get the counter into a local context as well, you could try
a factory function to return a tmp1 function with
local lookups from a closure.

     # untested!
     def get_tmp1(color):
         i = -1
         def tmp1(evald_s): # expect arg equivalent of eval(s) already done
             i += 1
             return QSimpleViewItem(i,color,evald_s)
         return tmp1

>
>    try:
>      myfile = open(filename)
>    except:
>      pass
>    else:
>      tmp = myfile.readlines()
>      print 'lines loaded'
>      datei.close()
       ^^^^^-- not myfile?

if you must eval the file lines (which I doubt, and which typically carries security dangers),
I'd try mapping eval by itself first, like
       tmp = map(eval,tmp)
then get the function from the factory
       tmp1 = get_tmp1(self.sv.lv.newColor) # gets the color once
>      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!
>
I suspect eval() is costing you a big part, unless QSimpleViewItem() has a
complicated constructor/init function. Spec the file line format in detail,
so we can eliminate eval.

Regards,
Bengt Richter



More information about the Python-list mailing list