extract elements of n char from a list
Fernando Perez
fperez528 at yahoo.com
Fri Jul 26 12:42:20 EDT 2002
Alex Martelli wrote:
> It's the best Python technique of all to learn to MEASURE rather
> than having to ASK. Set up a little benchmark script and use it.
> THEN you can ask here if the results aren't convincing to you (it's
> quite possible to get things wrong). Tim Peters' introduction
> to the Algorithms chapter of "Python Cookbook" (the printed edition
> that O'Reilly just launched) is invaluable in showing you exactly
> how to perform such little benchmarks for accuracy and solidity.
I Haven't had the chance to read the Cookbook yet, so I'm sure my solutions
may have holes in them. But coincidentally this morning I just finished
writing a benchmarking package for my own use, and since this kind of
question seems to come up often, I figured I'd post it for other's benefit.
Go to http://windom.colorado.edu/~fperez/python/ and click on the
benchmarking link.
It's a Linux/Unix package, as it implements clocks to accurately distinguish
between system and user time consumed by your process. Some examples taken
from the docs:
In [1]: from benchmark import timer
In [2]: def loop(n):
...: for i in range(n):
...: pass
...:
In [3]: timer(loop,100000)
Out[3]: 0.10000000000000009
In [4]: timer.report(loop,100000)
Timing report
-------------
Function tested : <function loop at 0x82afc04>
Number of calls : 1
Total execution time: 0.1 s
In [5]: timer.status()
Object: <benchmark.timing.GenericTimer instance at 0x82f43cc>
Object's __dict__ :
mode : user
ncalls : 1
print_args : 0
print_output: 0
stream : <open file '<stdout>', mode 'w' at 0x8100a50>
In [6]: timer.ncalls = 5
In [7]: timer.mode = 'all'
In [8]: timer.report(loop,100000)
Timing report
-------------
Function tested : <function loop at 0x82afc04>
Number of calls : 5
Total execution time:
system: 0.01 s
user : 0.52 s
Average time per call:
system: 0.002 s
user : 0.1 s
Well, I hope it's useful for some.
Cheers,
f.
More information about the Python-list
mailing list