Equivalent of Perl chomp?
Tim Peters
tim.one at home.com
Thu Jan 31 16:09:57 EST 2002
[Tim]
> Note that
>
> s.endswith('\n')
>
> is clearer than
>
> s[-1:] == '\n'
>
> For real fun, time them <wink>.
[Mark McEahern]
> I timed them with the following code and it occurred to me here's
> a possibly good case where aspect-oriented programming would be nice.
> Anyway, the results first:
>
> <function endsWith at 0x100f94d0> : 0.36 seconds.
> <function sliceIt at 0x100f9510> : 0.07 seconds.
Well, you're in for some fun of a kind I didn't have in mind <wink>. Do a
sanity check here: what's the clock rate on your box? How many cycles go
by in 0.07 seconds (let alone 0.36!)? Can you think of *any* non-insane way
to implement these operations that would take so bloody long?
I can't make time to explain timing subtleties today, but here's a hint:
try swapping the order of your calls to endsWith and sliceIt, and see what
you get then. If you hear your disk grinding <wink>, you might want to
contemplate the sensibility of using a 100MB string.
> The test:
>
> #! /usr/bin/env python
>
> import time
>
> def getString(n):
> s = "s" * n
> s = s + '\n'
> return s
>
> def endsWith(s):
> s.endswith('\n')
>
> def sliceIt(s):
> s[-1:] == '\n'
>
> def timeIt(func):
> n = 100000000
> s = getString(n)
> timeIn = time.time()
> func(s)
> timeOut = time.time()
> print "%s : %1.2f seconds." % (func, (timeOut - timeIn))
>
> timeIt(endsWith)
> timeIt(sliceIt)
More information about the Python-list
mailing list