Equivalent of Perl chomp?
Mark McEahern
marklists at mceahern.com
Thu Jan 31 15:29:06 EST 2002
Tim Peters wrote:
> Note that
>
> s.endswith('\n')
>
> is clearer than
>
> s[-1:] == '\n'
>
> For real fun, time them <wink>.
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.
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