Equivalent of Perl chomp?
Mark McEahern
marklists at mceahern.com
Thu Jan 31 18:39:12 EST 2002
Nicholas FitzRoy-Dale wrote:
> I think there are two issues that Tim is referring to:
> 1) time.time() is too low-resolution to accurately measure a single
> event, and
> 2) When you run the program, you must take into account a "cold"
> swap space, meaning that the first run of the program will be slower than
> subsequent runs.
>
> To avoid these things, it's best to run your test multiple times:
>
> startTime=time.time()
> for count in xrange (100000):
> testString.endswith('a')
> print time.time()-startTime
>
> Also run your program multiple times. To be thorough, run your program
> multiple times, swap the order of the tests, and then run the new, swapped
> program multiple times.
Thanks for the suggestions. I separated the two into separate files
(sliceit.py is shown below for what it's worth). Here's what I get now:
$ sliceit.py
<function sliceIt at 0x100f9420> : 0.46 seconds.
$ endswith.py
<function endsWith at 0x100f93f0> : 0.60 seconds.
This is after running each repeatedly in various orders.
Cheers,
// mark
#! /usr/bin/env python
# sliceit.py
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 = 1000
s = getString(n)
timeIn = time.time()
for x in xrange(100000):
func(s)
timeOut = time.time()
print "%s : %1.2f seconds." % (func, (timeOut - timeIn))
timeIt(sliceIt)
More information about the Python-list
mailing list