str.Template 4 times slower than calling replace multiple times

Discussion in 'Python' started by Jack Steven, Mar 9, 2009.

  1. Jack Steven

    Jack Steven Guest

    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)
     
    Jack Steven, Mar 9, 2009
    #1
    1. Advertising

  2. Jack Steven

    John Machin Guest

    On Mar 9, 3:15 pm, Jack Steven <> 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.
     
    John Machin, Mar 9, 2009
    #2
    1. Advertising

  3. Jack Steven

    Chris Rebert Guest

    On Sun, Mar 8, 2009 at 10:23 PM, John Machin <> wrote:
    > On Mar 9, 3:15 pm, Jack Steven <> 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
     
    Chris Rebert, Mar 9, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Rivers

    VS.NET is 10 times slower than VB6

    John Rivers, Aug 23, 2005, in forum: ASP .Net
    Replies:
    91
    Views:
    3,174
    =?Utf-8?B?TWFuaWthbmRhbg==?=
    Nov 2, 2005
  2. croeltgen
    Replies:
    1
    Views:
    511
    Andrew Thompson
    Oct 25, 2004
  3. Replies:
    15
    Views:
    526
    Roy Harvey
    Oct 26, 2006
  4. Philip Semanchuk

    Re: C-extension 2 times slower than exe

    Philip Semanchuk, Jun 23, 2009, in forum: Python
    Replies:
    3
    Views:
    418
  5. sanket
    Replies:
    7
    Views:
    1,042
    Tsung
    Nov 3, 2011
Loading...

Share This Page