Page - 124 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 124 -
Text of the Page - 124 -
124 5 SomeMorePythonEssentials
y = np.zeros(1000)
# ...use the function add
t = timeit.Timer(’for i in range(len(x)): x[i] = add(i, i+1)’, \
setup=’from __main__ import add, x’)
x_time = t.timeit(10000) # Time 10000 runs of the whole loop
print(’Time, function call: {:g} seconds’.format(x_time))
# ...no use of function add
t = timeit.Timer(’for i in range(len(y)): y[i] = i + (i+1)’, \
setup=’from __main__ import y’)
y_time = t.timeit(10000) # Time 10000 runs of the whole loop
print(’Time: {:g} seconds’.format(y_time))
What will happen here? Well, first of all, note that there are two calls to
timeit.Timer, one for each of the two loops from above. If we look at the first
call totimeit.Timer, i.e.,
t = timeit.Timer(’for i in range(len(x)): x[i] = add(i, i+1)’, \
setup=’from __main__ import add, x’)
we notice that two arguments are provided. You may recognize the first argument,
for i in range(len(x)): x[i] = add(i, i+1), asaone-lineversionof the
first loopfromabove, i.e. the loopoverx (usually,weprefer towritesuch loopsnot
onasingle line.However,whenusedasanargumentina functioncall likehere, the
one-line version is handy). This first argument, given as a string, is what we want
the timingof.Thesecondargument,setup=’from __main__ import add, x’,
is required for initialization, i.e., what the timer needs to do prior to timing of the
loop. If you lookcarefullyat the string-partof this second argument,younotice an
importstatementforaddandx.Youmaywonderwhyyouhavetodothatwhenthey
aredefined inyourcodeabove,but stay relaxedabout that, it is simply theway this
timer function works. What is required for the timer function to execute the code
given in the first argument, must be provided in the setup argument, even if it is
defined in thecodeabove.
The followingline,
x_time = t.timeit(10000) # Time 10000 runs of the whole loop
will cause the whole loop to actually be executed, not a single time, but 10000
times! There will be one recorded time, the time required to run the loop 10000
times. Thus, if an average time for a single run-through of the loop is desired, we
must divide the recorded time by (in this case) 10000. Often, however, the total
time is fine for comparison between alternatives. Theprint command brings the
recordedtime to the screen,before thenext loop is timed inanequivalentway.
Why is the loop run 10000 times? To get reliable timings, the execution times
must be on the order of seconds, that is why. How many times the requested code
snippet needs to be run, will of course depend on the code snippet in question.
Sometimes, a single execution is enough. Other times, many more executions than
10000 are required. Some trial and error is usually required to find an appropriate
number.
Executing theprogramproduces the followingresult,
Time, function call: 2.22121 seconds
Time: 1.4738 seconds
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
- Title
- Programming for Computations – Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Volume
- Second Edition
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2020
- Language
- English
- License
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 356
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik