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