On Tue, 16 Mar 2004 19:48:06 -0600, Skip Montanaro <sk**@pobox.com >
wrote:
For timing stuff like this I recommend you use timeit.py. Most people here
are getting used to looking at its output.
Excellent utility. This ought to be highlighted in the docs on the
time module, at least listed under "See also". I just grabbed the
first thing that came up and wrote my own little routine around the
clock() function.
[...]Post code and results and we'll be happy to throw darts... :-)
# PZfuncs.py -- Timing test for reloaded constants.
# Local constants:
h0 = 1
z1 = 1; z2 = 1
p1 = -1 +1j; p2 = -1 -1j; p3 = -1
pi = 3.1415926535897 931
import Constants
def h23null(freq):
pass
def h23a(freq):
s = complex(2*pi*fr eq)
return h0*(s-z1)*(s-z2)/((s-p1)*(s-p2)*(s-p3))
def h23b(freq):
z = Constants
s = complex(2*pi*fr eq)
return z.h0*(s-z.z1)*(s-z.z2)/((s-z.p1)*(s-z.p2)*(s-z.p3))
def h23c(freq):
h0 = Constants.h0
z1 = Constants.z1; z2 = Constants.z2
p1 = Constants.p1; p2 = Constants.p2; p3 = Constants.p3
s = complex(2*pi*fr eq)
return h0*(s-z1)*(s-z2)/((s-p1)*(s-p2)*(s-p3))
% timeit.py -s "from PZfuncs import * ; freq = 2.0" "h23null(fr eq)"
1000000 loops, best of 3: 0.461 usec per loop
% timeit.py -s "from PZfuncs import * ; freq = 2.0" "h23a(freq) "
100000 loops, best of 3: 4.94 usec per loop
% timeit.py -s "from PZfuncs import * ; freq = 2.0" "h23b(freq) "
100000 loops, best of 3: 5.79 usec per loop
% timeit.py -s "from PZfuncs import * ; freq = 2.0" "h23c(freq) "
100000 loops, best of 3: 6.29 usec per loop
My conclusion is that I should stick with form c in my application.
The savings from moving these assignments outside the function (form
a) does not justify the cost in possible problems after a reload. The
savings in going to form b is negligible. Form a is the easiest to
read, but forms b and c are not much worse.
These functions will typically be used in the interactive part of the
program to set up plots with a few hundred points. The time-consuming
computations are all done in the simulator, which is written in C++.
-- Dave