Python program execution time is measured in IPython

  • 2021-01-19 22:18:46
  • OfStack

When writing MATLAB scripts, I always use tic and toc to measure the running time of the program. There are occasional tests in Python, but they mostly rely on using the time module. When I got to IPython, I suddenly realized how easy it was to test the execution time of a program!

In IPython, program execution time is tested by magic functions. There are two magic functions for this function, one for time and one for timeit. This latter function is similar to the previous one, but is more accurate because the test is done by averaging multiple tests.

I have written a simple test script,


#!/usr/bin/python
 
import numpy as np
from numpy.randomimport randn
 
data = {i :randn() for i in range(7)}

The print(data) code is as follows:

The test records in IPython are as follows:


In [21]: %time%run dict.py
{0:1.1356172702418055, 1: -0.24725099335195655, 2: -0.8566028472732841, 3:-0.7027863981377108, 4: 0.8563383373116604, 5: 1.4790260114125025, 6:0.45741003038960254}
Wall time: 0 ns
 
In [22]: %time%run dict.py
{0:0.4634308244997993, 1: -0.2169481701227914, 2: 1.844213869777202, 3:-1.09428552819743, 4: -0.3162553722440559, 5: 0.35052990092285824, 6:-1.0779260478165211}
Wall time: 0 ns

The result is a little...

Indeed, such a simple statement can be executed for how long! Moreover, the book is now used or standard pressure processor, and I7 calculation of the strongest core. OK, now change 1 to loop:


#!/usr/bin/python
 
import numpy as np
from numpy.randomimport randn
 
for i inrange(1000):
data = {i : randn() for i in range(7)}
print(data)

After the above code is stored in a new file, it is tested and recorded in IPython. A large chunk of output flashed in front of my eyes. It is not possible to copy all the records, but the partial result is as follows:


{0:-0.8346562430694008, 1: -0.5081226699243429, 2: 0.14690620427134915, 3:-1.1947018796604227, 4: 0.5299884594565932, 5: -0.11730239691529774, 6:-0.008304349615949396}
{0:-0.5004558540946741, 1: -2.239882398599743, 2: -0.4877611466394901, 3:0.04679029941320335, 4: -0.04061984884439187, 5: -0.18026780798066566, 6:0.2617579789690715}
{0:-0.8498496249579838, 1: -0.34650772255315343, 2: -0.7067822075542513, 3:0.4675343777714329, 4: -2.095049716609193, 5: -1.9396619017424426, 6:1.4723754138476228}
{0:1.0829454562962688, 1: 0.3658593642766029, 2: 0.7825005873884392, 3:-0.7024245957641886, 4: -0.9083494908408439, 5: -0.5225361343604294, 6:0.2780526056846729}
Wall time: 2.67 s

The result of this implementation is indeed quite long, personally think the main bottleneck should be in the output function! In test 1 with timeit, see if there is a big change in the results. Part of the record results are as follows:


{0:1.1881922773474327, 1: 2.095703415950821, 2: 0.7768251617416795, 3:-0.3639801567794642, 4: -1.2155069020886828, 5: 0.05454831526380187, 6:0.521994301720664}
{0:0.0962573073179745, 1: -0.6917641905037167, 2: 1.021197433972855, 3:0.4155701479521505, 4: 2.393391538898768, 5: 1.3755258048747323, 6:-0.5540780961303758}
{0:-0.418199398478115, 1: 1.1973929026808094, 2: -0.3243683593668846, 3:-1.7765735471011064, 4: -1.1567528174241677, 5: -2.297151750515544, 6:1.6966820033283279}
1 loop, best of 3:1.68 s per loop

As you can see from the above result, it seems that this result is also not taking the mean (the tutorial I read said taking the mean). In the result prompt above, the test was run three times and the best result was taken out of the three times. There is a definite difference from the previous results. I will modify 1 code, change the print, see if print is a big time user! The code is as follows:


#!/usr/bin/python
 
import numpy as np
from numpy.randomimport randn
 
for i inrange(1000):
data = {i : randn() for i in range(7)}
a = data

The results of the two tests are as follows:

Time test:


In [28]: %time%run dict_loop_no_disp.py
Wall time: 15 ms
In [29]: %timeit%run dict_loop_no_disp.py
100 loops, best of3: 3.2 ms per loop

As can be seen from the above results, print is indeed a big time user! From this set of results, it seems that my understanding of the output of timeit is a little biased. Intuitively, the last test seemed to have only one loop test, but produced three best scores. And this test went through 100 loops and got 3 best scores? Is that the understanding?

How to understand and do not go to deep research, overall speaking is the back 1 kind of accurate 1 point just! In actual use, the use of time is about not much, after all, my Python program execution time is almost negligible.


Related articles: