|
1 | | -.. _getting_started: |
| 1 | +.. _getting_started: |
2 | 2 | .. include:: ./ext_links.txt |
3 | 3 |
|
4 | 4 | Getting Started |
@@ -81,9 +81,63 @@ Also give very short introduction to what kind of code Numba/Intel® SDC can com |
81 | 81 | Measuring Performance |
82 | 82 | ##################### |
83 | 83 |
|
84 | | -.. todo:: |
85 | | - Short intro how to measure performance. Compilation time and run time. Illustrate by example. Reference to relevant discussion in Numba documentation |
86 | | - |
| 84 | +.. 1. Short intro how to measure performance. |
| 85 | +
|
| 86 | +Lets consider we want to measure performance of Series.max() method. |
| 87 | + |
| 88 | +.. code:: |
| 89 | +
|
| 90 | + from numba import njit |
| 91 | +
|
| 92 | + @njit |
| 93 | + def series_max(s): |
| 94 | + return s.max() |
| 95 | +
|
| 96 | +.. 2. Compilation time and run time. |
| 97 | +
|
| 98 | +First, recall that Intel® SDC is based on Numba. Therefore, execution time may consist of the following: |
| 99 | + 1. Numba has to *compile* your function for the first time, this takes time. |
| 100 | + 2. *Boxing* and *unboxing* convert Python objects into native values, and vice-versa. They occur at the boundaries of calling a `Numba*`_ function from the Python interpreter. E.g. boxing and unboxing apply to `Pandas*`_ types like :ref:`Series <pandas.Series>` and :ref:`DataFrame <pandas.DataFrame>`. |
| 101 | + 3. The execution of the *function itself*. |
| 102 | + |
| 103 | +A really common mistake when measuring performance is to not account for the above behaviour and |
| 104 | +to time code once with a simple timer that includes the time taken to compile your function in the execution time. |
| 105 | + |
| 106 | +A good way to measure the impact Numba JIT has on your code is to time execution using |
| 107 | +the `timeit <https://docs.python.org/3/library/timeit.html>`_ module functions. |
| 108 | + |
| 109 | +Intel® SDC also recommends eliminate the impact of compilation and boxing/unboxing by measuring the time inside Numba JIT code. |
| 110 | + |
| 111 | +.. 3. Illustrate by example. |
| 112 | +
|
| 113 | +Example of measuring performance: |
| 114 | + |
| 115 | +.. code:: |
| 116 | +
|
| 117 | + import time |
| 118 | + import numpy as np |
| 119 | + import pandas as pd |
| 120 | + from numba import njit |
| 121 | +
|
| 122 | + @njit |
| 123 | + def perf_series_max(s): # <-- unboxing |
| 124 | + start_time = time.time() # <-- time inside Numba JIT code |
| 125 | + res = s.max() |
| 126 | + finish_time = time.time() # <-- time inside Numba JIT code |
| 127 | + return finish_time - start_time, res # <-- boxing |
| 128 | +
|
| 129 | + s = pd.Series(np.random.ranf(size=100000)) |
| 130 | + exec_time, res = perf_series_max(s) |
| 131 | + print("Execution time in JIT code: ", exec_time) |
| 132 | +
|
| 133 | +.. 4. Reference to relevant discussion in Numba documentation. |
| 134 | +
|
| 135 | +See also `Numba*`_ documentation `How to measure the performance of Numba? <http://numba.pydata.org/numba-doc/latest/user/5minguide.html#how-to-measure-the-performance-of-numba>`_ |
| 136 | + |
| 137 | +.. 5. Link to performance tests. |
| 138 | +
|
| 139 | +See also Intel® SDC repository `performance tests <https://github.com/IntelPython/sdc/tree/master/sdc/tests/tests_perf>`_. |
| 140 | + |
87 | 141 | What If I Get Poor Performance? |
88 | 142 | ############################### |
89 | 143 |
|
|
0 commit comments