array Performance: Introducing %timeit¶array operations execute significantly faster than corresponding list operations%timeit magic command times the average duration of operationsimport random
%timeit rolls_list = \
[random.randrange(1, 7) for i in range(0, 6_000_000)]
%timeit executes a statement in a loop, and it runs the loop seven times%timeit chooses an appropriate value%timeit displays the statement’s average execution time, as well as the standard deviation of all the executionsarray Containing Results of 6,000,000 Die Rolls¶import numpy as np
%timeit rolls_array = np.random.randint(1, 7, 6_000_000)
%timeit rolls_array = np.random.randint(1, 7, 60_000_000)
%timeit rolls_array = np.random.randint(1, 7, 600_000_000)
%timeit -n3 -r2 rolls_array = np.random.randint(1, 7, 6_000_000)
IPython provides dozens of magics for a variety of tasks—for a complete list, see the IPython magics documentation. Here are a few helpful ones:
%load to read code into IPython from a local file or URL.%save to save snippets to a file.%run to execute a .py file from IPython.%precision to change the default floating-point precision for IPython outputs.%cd to change directories without having to exit IPython first.%edit to launch an external editor—handy if you need to modify more complex snippets. %history to view a list of all snippets and commands you’ve executed in the current IPython session.©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.
DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.