Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > str.Template 4 times slower than calling replace multiple times

Reply
Thread Tools

str.Template 4 times slower than calling replace multiple times

 
 
Jack Steven
Guest
Posts: n/a
 
      03-09-2009
Isn't string.Template suppose to be faster than calling replace multiple
times? That's what I thought until I benchmarked this code, where
string.Template ended up being 4 times slower.

This doesn't make sense to me, since unlike when you are calling replace
multiple times, code behind Template can scan the string only once, and it
doesn't have to allocate multiple strings. So why is slower? Is there a way
to improve it's speed?


from string import Template
from time import time

def test1(format, user, channel, message):
nick, id, host = user
s = format
s = s.replace('$nick', nick)
s = s.replace('$id', id)
s = s.replace('$host', host)
s = s.replace('$channel', channel)
s = s.replace('$message', message)

def test2(format, user, channel, message):
nick, id, host = user
s = Template(format)
d = {
'nick': nick,
'id': id,
'host': host,
'channel': channel,
'message': message,
}
s = s.substitute(d)

user = ('jacks-', 'id', '127.0.0.1')
channel = '#test'
message = 'this is a message'
format = '<$nick@$host> $message'

for test in (test1, test2):
start = time()
for i in xrange(100000):
test(format, user, channel, message)
end = time()
print 'Done in %f seconds' % (end - start)
 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      03-09-2009
On Mar 9, 3:15*pm, Jack Steven <a...@hotmail.com> wrote:
> Isn't string.Template suppose to be faster than calling replace multiple
> times? That's what I thought until I benchmarked this code, where
> string.Template ended up being 4 times slower.
>
> This doesn't make sense to me, since unlike when you are calling replace
> multiple times, code behind Template can scan the string only once, and it
> doesn't have to allocate multiple strings. So why is slower? Is there a way
> to improve it's speed?
>
> from string import Template
> from time import time
>
> def test1(format, user, channel, message):
> * * nick, id, host = user
> * * s = format
> * * s = s.replace('$nick', nick)
> * * s = s.replace('$id', id)
> * * s = s.replace('$host', host)
> * * s = s.replace('$channel', channel)
> * * s = s.replace('$message', message)
>
> def test2(format, user, channel, message):
> * * nick, id, host = user
> * * s = Template(format)
> * * d = {
> * * * * 'nick': nick,
> * * * * 'id': id,
> * * * * 'host': host,
> * * * * 'channel': channel,
> * * * * 'message': message,
> * * }
> * * s = s.substitute(d)
>
> user * *= ('jacks-', 'id', '127.0.0.1')
> channel = '#test'
> message = 'this is a message'
> format *= '<$nick@$host> $message'
>
> for test in (test1, test2):
> * * start = time()
> * * for i in xrange(100000):
> * * * * test(format, user, channel, message)
> * * end = time()
> * * print 'Done in %f seconds' % (end - start)


A couple of points:
(a) string.Template is written in Python, not C so (1) it runs slower
(2) you can get some of your answer from the source on your computer
(b) It has to be smarter than shotgun replaces ... You may know that
your template contains "$nick" and not "$nick $nickname $nickers" but
it doesn't.
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      03-09-2009
On Sun, Mar 8, 2009 at 10:23 PM, John Machin <> wrote:
> On Mar 9, 3:15Â*pm, Jack Steven <a...@hotmail.com> wrote:
>> Isn't string.Template suppose to be faster than calling replace multiple
>> times? That's what I thought until I benchmarked this code, where
>> string.Template ended up being 4 times slower.
>>
>> This doesn't make sense to me, since unlike when you are calling replace
>> multiple times, code behind Template can scan the string only once, and it
>> doesn't have to allocate multiple strings. So why is slower? Is there a way
>> to improve it's speed?
>>
>> from string import Template
>> from time import time
>>
>> def test1(format, user, channel, message):
>> Â* Â* nick, id, host = user
>> Â* Â* s = format
>> Â* Â* s = s.replace('$nick', nick)
>> Â* Â* s = s.replace('$id', id)
>> Â* Â* s = s.replace('$host', host)
>> Â* Â* s = s.replace('$channel', channel)
>> Â* Â* s = s.replace('$message', message)
>>
>> def test2(format, user, channel, message):
>> Â* Â* nick, id, host = user
>> Â* Â* s = Template(format)
>> Â* Â* d = {
>> Â* Â* Â* Â* 'nick': nick,
>> Â* Â* Â* Â* 'id': id,
>> Â* Â* Â* Â* 'host': host,
>> Â* Â* Â* Â* 'channel': channel,
>> Â* Â* Â* Â* 'message': message,
>> Â* Â* }
>> Â* Â* s = s.substitute(d)
>>
>> user Â* Â*= ('jacks-', 'id', '127.0.0.1')
>> channel = '#test'
>> message = 'this is a message'
>> format Â*= '<$nick@$host> $message'
>>
>> for test in (test1, test2):
>> Â* Â* start = time()
>> Â* Â* for i in xrange(100000):
>> Â* Â* Â* Â* test(format, user, channel, message)
>> Â* Â* end = time()
>> Â* Â* print 'Done in %f seconds' % (end - start)

>
> A couple of points:
> (a) string.Template is written in Python, not C so (1) it runs slower
> (2) you can get some of your answer from the source on your computer
> (b) It has to be smarter than shotgun replaces ... You may know that
> your template contains "$nick" and not "$nick $nickname $nickers" but
> it doesn't.


IOW, the comparison being made is fatally flawed, hence the
unfavorable but incorrect results.

Cheers,
Chris

--
I have a blog:
http://blog.rebertia.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: HELP! Ping getting slower and slower and slower! Paul Computer Information 2 04-03-2012 05:58 PM
Re: C-extension 2 times slower than exe Philip Semanchuk Python 3 06-25-2009 10:49 PM
Query 10 times slower (and results reordered!) from ASP.NET page than console app. wizofaus@hotmail.com ASP .Net 15 10-25-2006 11:24 PM
VS.NET is 10 times slower than VB6 John Rivers ASP .Net 91 11-02-2005 05:55 PM
Release version 30 times slower than debug version croeltgen Java 1 10-24-2004 11:49 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57