Web-Books
in the Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Page - 123 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

Page - 123 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Image of the Page - 123 -

Image of the Page - 123 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Text of the Page - 123 -

5.6 MeasuringExecutionTime 123 5.6 MeasuringExecutionTime Even though computational speed should have low priority among beginners to programming, it might be useful, at least, to have seen how execution time can be found for some code snippet. This is relevant for more experienced programmers, when it is required tofindaparticularly fast codealternative. The measuring of execution time is complicated by the fact that a number of background processes (virus scans, check for new mail, check for software updates, etc.) will affect the timing. To some extent, it is possible to turn off such background processes, but that strategy soon gets too complicated for most of us. Fortunately, simpler and safer tools are available. To find the execution time of small code snippets, a good alternative is to use the timeitmodule12 from the Pythonstandard library. 5.6.1 ThetimeitModule Todemonstratehowthismodulemaybeused,wewill investigatehowfunctioncalls affect execution time. Our brief β€œinvestigation” is confined to the filling of an array with integers, done with and without a particular function call. The details are best explainedwith reference to the followingcode(no timingyet!): import numpy as np def add(a, b): return a + b x = np.zeros(1000) y = np.zeros(1000) for i in range(len(x)): x[i] = add(i, i+1) # use function call to fill array for i in range(len(x)): y[i] = i + (i+1) # ...no function call So,thesumoftwointegersisassignedtoeacharrayelement.Thearraysxandywill containexactly thesamenumberswhen thesecondloop isfinished,but tofillx,we useacall to the functionadd.Thus, the time tofillx is expectedto take longer than filling y, which just adds the numbers directly. Our question is, how much longer does it take touse the functioncall? To answer this question by use of timeit, we may write the script timing_function_call.py: import timeit import numpy as np def add(a, b): return a + b x = np.zeros(1000) 12 https://docs.python.org/3/library/timeit.html.
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition"
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
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python