Seite - 148 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 148 -
Text der Seite - 148 -
148 6 ComputingIntegralsandTestingCode
6.4.3 SpeedupGainedwithVectorization
Nowthatwehavecreatedfaster,vectorizedversionsof thefunctions, it isof interest
tomeasurehowmuchfaster theyare.Restrictingourselves to themidpointmethod,
wemightproceedas:
import timeit
from integration_methods_vec import midpoint as midpoint_vec
from midpoint import midpoint
from numpy import exp
v = lambda t: 3*t**2*exp(t**3)
t = timeit.Timer(’midpoint(v, 0, 1, 1000000)’, \
setup=’from __main__ import midpoint, v’)
time_midpoint = t.timeit(10)
print(’Time, midpoint: {:g} seconds’.format(time_midpoint))
# Vectorized version
t = timeit.Timer(’midpoint_vec(v, 0, 1, 1000000)’, \
setup=’from __main__ import midpoint_vec, v’)
time_midpoint_vec = t.timeit(10)
print(’Time, midpoint vec: {:g} seconds’.format(time_midpoint_vec))
print(’Efficiency factor: {:g}’.format(time_midpoint/time_midpoint_vec))
Running theprogramgives
Time, midpoint: 19.6083 seconds
Time, midpoint vec: 0.868379 seconds
Efficiency factor: 22.5804
We see that the vectorized version is about 20 times faster. The results for the
trapezoidal method are very similar, and the factor of about 20 is independent of
thenumberof intervals.
6.5 RateofConvergence
We have seen that the numerical integration error drops when the number of sub-
intervalsn is increased (causing each sub-interval to become smaller). This is fine
and in linewithourexpectations,but some importantdetails shouldbe added.
Asymptotic Behavior of the Integration Error It is known that, if only the size
hof thesub-intervals is small enough,numerical integrationmethodstypicallygive
anerror
E=Khr , (6.21)
whereK is anunknownconstant,while theconvergencerate r is aknownconstant
that depends on the method. When a method has convergence rate r, it is known
as an r-th order method. A large r is beneficial, sinceE then drops quicker when
h→0.
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Band
- Second Edition
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2020
- Sprache
- englisch
- Lizenz
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 356
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik