Help with optimisation

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Mon Aug 13 12:10:05 EDT 2007


special_dragonfly a écrit :
> Hello,
(snip)
> The function doesn't return anything, but it's called often enough and 
> depending on the optimisation I'll be able to use the same style in other 
> areas of the program.
> 
> previous code:
> def CreatePerson(text_buffer):
>     dom=xml.dom.minidom.parseString(text_buffer)
>     reflist = dom.getElementsByTagName('Country')
>     Country = reflist[0].firstChild.nodeValue
>     reflist = dom.getElementsByTagName('Age')
>     Age = reflist[0].firstChild.nodeValue
>     reflist = dom.getElementsByTagName('Surname')
>     Surname = reflist[0].firstChild.nodeValue
>     reflist = dom.getElementsByTagName('Forename')
>     Forename = reflist[0].firstChild.nodeValue
>     cursor.execute('INSERT INTO Person VALUES(?,?,?)', (Forename + "-" + 
> Surname, Age, Country))
>     connection.commit()
> 
> I've changed it now to this:
> def CreatePerson(text_buffer):
>     dom=xml.dom.minidom.parseString(text_buffer)
>     elements=['Country','Age','Surname','Forename']
>     Values=[]
>     for element in elements:
>         reflist=dom.getElementsByTagName(element)
>         Values.append(reflist[0].firstChild.nodeValue)
>         # I can get away with the above because I know the structure of the 
> XML
>     cursor.execute('INSERT INTO Person 
> VALUES(?,?,?)',(Forename+"-"+Surname,Age,Country))
>     connection.commit()

A common python optimisation trick is to stote local references to save 
on attribute lookup time, ie:

# local ref to parseString
import dom
dom_parseString=xml.dom.minidom.parseString

def CreatePerson(text_buffer):
     dom = dom_parseString(text_buffer)
     elements=['Country','Age','Surname','Forename']
     values=[]
     getElementByTagName = dom.getElementsByTagName
     for element in elements:
         reflist = getElementsByTagName(element)
         values.append(reflist[0].firstChild.nodeValue)


But as Alex already pointed out, you'd be better using (c)ElementTree.

> They both seem ugly IMO (read: longer than intuitively necessary),

I'd say this is a common problem with XML :-/



More information about the Python-list mailing list