You Are Here:

Community: Developer Discussion Boards

#1 Old problems with string interpolation speed - 2009-03-12, 23:41

Join Date: Mar 2009
Posts: 8
zarzu
Offline
Registered User
i am currently coding a rest server for the n95, the server let's you access sensor data (gps, wlan, bluetooth etc) stored in a e32db, which is all running smooth so far, well except for the serializing. the service returns xml by default, kml, json and rss on demand, i have templates for each of them, they consist of up to 3 files for each format, i load them in variables and then build the outgoing file by concatenating them after inserting the values, so the files may look something like this:
Code:
template1:
<?xml>
<root sensor="%(sensor)s">
%(entries)s
</root>

template2:
 <entry name="%(name)s" %(otherattributes)s>
  %(sometext)s
 </entry>
and they are processed as follows:
Code:
tmpl1 = load template1
tmpl2 = load template2

entries = ""
for entry in dbrows:
 otherattributes = "date=\"somedate\""
 name = entry["name"]
 sometext = entry["description"]
 
 entries += tmpl2 % {"name": name, "otherattributes": otherattributes, "sometext": sometext}

output = tmpl1 % {"sensor": sensor, "entries": entries}
return output
this seems to be the straight forward thing to do for me, but the problem is for a 30kb file this is taking up to 25 seconds, which is insanely long, so there must be something that is slowing this down terribly, i have no prior experience with python and i didn't find anything related to my problem.
i'd appreciate any help here. thanks in advance.
Reply With Quote

#2 Old Re: problems with string interpolation speed - 2009-03-13, 01:58

Join Date: Nov 2007
Posts: 318
Location: Sertaozinho/Brazil
Send a message via MSN to marcelobarrosalmeida Send a message via Skype™ to marcelobarrosalmeida
marcelobarrosalmeida's Avatar
marcelobarrosalmeida
Online
Forum Nokia Champion
Hum, nearly 1 package per second ... so, could you show your transmission routine ?

Marcelo


Marcelo Barros
Nokia E71, N800, N95 and XM 5800
http://www.croozeus.com
http://wordmobi.wordpress.com
http://jedizone.wordpress.com
Reply With Quote

#3 Old Re: problems with string interpolation speed - 2009-03-13, 07:47

Join Date: Mar 2009
Posts: 8
zarzu
Offline
Registered User
the transmission is handled by the mobile web server and i am not talking about how long the whole request takes to come back, the 25 seconds come from taking the time before and after serializing, it's sent as an attribute of the root element of the xml file.
Reply With Quote

#4 Old Re: problems with string interpolation speed - 2009-03-13, 09:28

Join Date: Mar 2003
Posts: 937
Location: Espoo, Finland
JOM's Avatar
JOM
Offline
Forum Nokia Champion
Your system is very complicated, so "naturally" it requires a lot of time. Templates with 3 files each: file system access is slow. GPS requires several seconds to activate even under the most optimal circumstances, several minutes usually, even 15 mins without Assistance (A-GPS). Even importing socket requires seconds.

If you're not happy with performance, maybe you should measure where all the time is spend? Then you can consider whether you want to simplify your system in some places or use lazy import.

Here's a lazy profiler:

Code:
# Lazy import, after user looking at screen:
#import time
#t0 = time.clock()
import socket
import urllib
#print "Time: %f" % (time.clock() - t0)
# 5 seconds in emulator DEBUG mode
# 3.8 seconds in emulator NON_DEBUG mode
Cheers,

--jouni
Reply With Quote

#5 Old Re: problems with string interpolation speed - 2009-03-13, 17:04

Join Date: Mar 2009
Posts: 8
zarzu
Offline
Registered User
i have to correct myself a little, it seems to take 25 seconds for a 200kb file, someone else checked the size and obviously it wasn't right, so this doesn't feel that horrible anymore, but still i feel like it's taking too long, on the other hand i don't really have any comparisons, but still.

i have to reiterate though, the time i measured is only dictionary access, string interpolation and string concatenation, imports are done at the beginning, this has no effect, file loading takes 0.02 seconds, sensors (like gps) have no impact here, i read them with a separate program that writes them to a db.

i can give some measurements of the other parts of this program, for a 500kb output, it takes 6s to read the database and write all rows in a list, 2s to convert all of those rows into dictionaries and then 70s to write these rows into xml. and that jump just seems out of whack to me.
Reply With Quote

#6 Old Re: problems with string interpolation speed - 2009-03-13, 20:50

Join Date: May 2004
Posts: 524
Location: Tampere, Finland
jethro.fn's Avatar
jethro.fn
Offline
Forum Nokia Champion
Quote:
Originally Posted by zarzu View Post
Code:
entries = ""
for entry in dbrows:
  ...
  entries += ...
This is always a bad idea in Python or any other language with immutable strings (Java, .NET). What is happening here is that for each dbrows entry, a new entries string is created and the old one destroyed. The new entries string and the old one are bigger and bigger each round, slowing down the execution due to unnecessary pressure on memory allocator and garbage collector of Python.

Here's how you usually do it in Python:

Code:
entries = []
for entry in dbrows:
  ...
  entries.append(...)

entries_joined = "".join(entries)
Reply With Quote

#7 Old Re: problems with string interpolation speed - 2009-03-13, 23:06

Join Date: Mar 2009
Posts: 8
zarzu
Offline
Registered User
i really hoped that was it, tried it, but it actually increased the time needed for processing by 10%

correction: i was a bit over enthusiastic with using the join it seems, i only used it on the really large concatenations now and it's running 10% faster than the original version.
thanks for the tipp!
Last edited by zarzu : 2009-03-13 at 23:12.
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
Thread Thread Starter Forum Replies Last Post
illegal unicode escape in j2me when i splite string... chaoraksa Mobile Java General 36 2009-02-04 09:56
org.xmlpull.v1.XmlPullParserException: expected: START_TAG {null} for1nall Mobile Java General 0 2009-01-02 10:50
TextEditor with two errors ahashim Personal Profile 0 2006-08-30 00:43
Convert bytes in String decisor Mobile Java General 8 2006-08-03 19:56
global root overflow(please help) sushant_125 Mobile Java Networking & Messaging & Security 3 2004-05-14 09:24

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia