Web-Books
im Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Seite - 123 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

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

Bild der Seite - 123 -

Bild der Seite - 123 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Text der Seite - 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.
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition"
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
Web-Books
Bibliothek
Datenschutz
Impressum
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python