[PATCH] move hgclient._eatlines to util

Idan Kamara idankk86 at gmail.com
Tue Aug 9 15:56:36 CDT 2011


On Tue, Aug 9, 2011 at 11:30 PM, Matt Mackall <mpm at selenic.com> wrote:
>
> On Tue, 2011-08-09 at 23:14 +0300, Idan Kamara wrote:
> > # HG changeset patch
> > # User Idan Kamara <idankk86 at gmail.com>
> > # Date 1312920870 -10800
> > # Node ID dea52d1d298573c16285e822ed6161f7e1154460
> > # Parent  ecf36f339f6802afbc9db057afd2df84ec433e86
> > move hgclient._eatlines to util
> >
> > and use cStringIO for faster line iteration
>
> Please send independent changes as independent patches, _especially_
> when code movement is involved.

Ok.

>
> Also, I suspect your cStringIO claim above is not based on an actual
> measurement.

I guess I should know better not to write the word "fast" without proving
it to the doubters....

I wasn't really sitting on this thinking "how can I make this
go faster?" When I moved this function over to util, this version just
looked better
and happened to be faster.

import cStringIO, timeit

def eatlines1(s, n):
    idx = 0
    for i in xrange(n):
        idx = s.find('\n', idx) + 1
    return s[idx:]

def eatlines2(s, n):
     cs = cStringIO.StringIO(s)
     for line in cs:
         n -= 1
         if n == 0:
             return cs.read()
     return ''

print timeit.timeit("eatlines1('aaaaaa\\n'*20, 19)", "from __main__ import
eatlines1")
print timeit.timeit("eatlines2('aaaaaa\\n'*20, 19)", "from __main__ import
eatlines2")

$ python eatlines.py
5.66405916214
3.22999715805
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://selenic.com/pipermail/mercurial-devel/attachments/20110809/aa9324df/attachment.html>


More information about the Mercurial-devel mailing list