473,382 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Help with optimisation

Hello,
I know this might be a little cheeky, and if it is, please say, but I need a
little hand optimising some code. For the simple reason that this is
'company' code and I have no idea what I'm allowed to release and not as the
case may be I've changed anything that could give an indication of the
company - if that makes any sense...

for the code below:
text_buffer is a single record from an XML stream. I can't read in the
entire XML at once because it isn't all available straight away, so I
capture line by line, and when a full message is available I use parseString
under the minidom API.
The SQL version is SQLite. It was recommended to me, and is adequate for the
uses I put it to.
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()

They both seem ugly IMO (read: longer than intuitively necessary), and so I
was wondering whether there was any way to combine Forename and Surname
together within the Values list (think merge cells with the '-' in between)
so I could use the unary(?) operator within the SQL?

I suppose if this is a cheeky request then I won't get any replies.
Thank you for any help
Dominic

Aug 13 '07 #1
2 1235
special_dragonfly <Do*****@PLEASEASK.co.ukwrote:
...
dom=xml.dom.minidom.parseString(text_buffer)
If you need to optimize code that parses XML, use ElementTree (some
other parsers are also fast, but minidom ISN'T).
Alex
Aug 13 '07 #2
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 :-/
Aug 13 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: OPQ | last post by:
Hi all, I'd happy to have you share some thougts about ultimate optimisations on those 2 topics: (1)- adding one caractere at the end of a string (may be long) (2)- in a dict mapping a key...
2
by: Simon Elliott | last post by:
What optimisation do compilers typically provide when passing STL containers around? For example, if I do something like this: struct Tbloggs { std::string s1; }; typedef...
16
by: simonwittber | last post by:
Hello People. I've have a very tight inner loop (in a game app, so every millisecond counts) which I have optimised below: def loop(self): self_pool = self.pool self_call_exit_funcs =...
8
by: Jon Maz | last post by:
Hi, I'm facing a code-optimisation issue on an asp.net/vb.net/SQL Server 2000 project. A web page containing not much more than 3 DropDownLists is taking nigh on 6 seconds to load, because each...
6
by: Lee Harr | last post by:
I have a database where I remove the schema public. When I try to use the createlang script, it fails like this ... >createdb foo CREATE DATABASE >psql foo -c "select version()" version...
1
by: David Welch | last post by:
Hi, I have a bit of code where I am relying on empty base member optimisation. The bit of code is below: template<typename Enum> struct EncodePrefix { template<Enum e> struct Apply
1
by: grid | last post by:
Hi, I was exploring the affect of cache on program performance/optimisation.Is it the compilers responsibility only to consider this kind of optimisation or the programmer can do his bit in this...
21
by: c | last post by:
Hi everybody. I'm working on converting a program wriiten on perl to C, and facing a problem with concatenate strings. Now here is a small program that descripe the problem, if you help me to...
41
by: c | last post by:
Hi every one, Me and my Cousin were talking about C and C#, I love C and he loves C#..and were talking C is ...blah blah...C# is Blah Blah ...etc and then we decided to write a program that...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.