|
1 |
| -# benchmark_utils |
| 1 | +# Benchmark utils |
| 2 | + |
| 3 | +Utils for benchmark - wrapper over python timeit. |
| 4 | + |
2 | 5 | [](https://pypi.org/project/benchmark-utils/)
|
3 | 6 | [](https://badge.fury.io/py/benchmark-utils)
|
4 | 7 | [](https://github.com/ayasyrev/benchmark_utils/actions?workflow=Tests) [](https://codecov.io/gh/ayasyrev/benchmark_utils)
|
5 |
| -Tested on python 3.8, 3.9, 3.10, 3.11 |
6 |
| - |
7 |
| -Utils for benchmark. |
| 8 | + |
| 9 | +Tested on python 3.7 - 3.11 |
| 10 | + |
| 11 | +## Install |
| 12 | + |
| 13 | +Install from pypi: |
| 14 | + |
| 15 | +`pip install benchmark_utils` |
| 16 | + |
| 17 | +Or install from github repo: |
| 18 | + |
| 19 | +`pip install git+https://github.com/ayasyrev/benchmark_utils.git` |
| 20 | + |
| 21 | +## Basic use. |
| 22 | + |
| 23 | +Lets benchmark some (dump) functions. |
| 24 | + |
| 25 | + |
| 26 | +```python |
| 27 | +from time import sleep |
| 28 | + |
| 29 | +def func_to_test_1(sleep_time: float = 0.1, mult: int = 1) -> None: |
| 30 | + """simple 'sleep' func for test""" |
| 31 | + sleep(sleep_time * mult) |
| 32 | + |
| 33 | + |
| 34 | +def func_to_test_2(sleep_time: float = 0.11, mult: int = 1) -> None: |
| 35 | + """simple 'sleep' func for test""" |
| 36 | + sleep(sleep_time * mult) |
| 37 | + |
| 38 | +``` |
| 39 | + |
| 40 | +Let's create benchmark. |
| 41 | + |
| 42 | + |
| 43 | +```python |
| 44 | +from benchmark_utils import Benchmark |
| 45 | +``` |
| 46 | + |
| 47 | + |
| 48 | +```python |
| 49 | +bench = Benchmark( |
| 50 | + [func_to_test_1, func_to_test_2], |
| 51 | +) |
| 52 | +``` |
| 53 | + |
| 54 | + |
| 55 | +```python |
| 56 | +bench |
| 57 | +``` |
| 58 | +<details open> <summary>output</summary> |
| 59 | + <pre>Benchmark(func_to_test_1, func_to_test_2)</pre> |
| 60 | +</details> |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +Now we can benchmark that functions. |
| 65 | + |
| 66 | + |
| 67 | +```python |
| 68 | +bench() |
| 69 | +``` |
| 70 | +<details open> <summary>output</summary> |
| 71 | + <pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"> Func name | Sec <span style="color: #800080; text-decoration-color: #800080">/</span> run |
| 72 | +</pre> |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">func_to_test_1: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.10</span> <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.0</span>% |
| 78 | +</pre> |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">func_to_test_2: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.11</span> <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">-9.6</span>% |
| 84 | +</pre> |
| 85 | + |
| 86 | +</details> |
| 87 | + |
| 88 | + |
| 89 | +We can run it again, all functions, some of it, exclude some and change number of repeats. |
| 90 | + |
| 91 | + |
| 92 | +```python |
| 93 | +bench.run(num_repeats=10) |
| 94 | +``` |
| 95 | +<details open> <summary>output</summary> |
| 96 | + <pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"> Func name | Sec <span style="color: #800080; text-decoration-color: #800080">/</span> run |
| 97 | +</pre> |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">func_to_test_1: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.10</span> <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.0</span>% |
| 103 | +</pre> |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">func_to_test_2: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.11</span> <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">-8.8</span>% |
| 109 | +</pre> |
| 110 | + |
| 111 | +</details> |
| 112 | + |
| 113 | + |
| 114 | +After run, we can prunt results - sorted or not, reversed, compare results with best or not. |
| 115 | + |
| 116 | + |
| 117 | +```python |
| 118 | +bench.print_results(reverse=True) |
| 119 | +``` |
| 120 | +<details open> <summary>output</summary> |
| 121 | + <pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"> Func name | Sec <span style="color: #800080; text-decoration-color: #800080">/</span> run |
| 122 | +</pre> |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | +<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">func_to_test_2: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.11</span> <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.0</span>% |
| 128 | +</pre> |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">func_to_test_1: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.10</span> <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">9.7</span>% |
| 134 | +</pre> |
| 135 | + |
| 136 | +</details> |
| 137 | + |
| 138 | + |
| 139 | +We can add functions to bencmark as list of funtions (or partial) ar as dictionary: `{"name": function}`. |
| 140 | + |
| 141 | + |
| 142 | +```python |
| 143 | +bench = Benchmark([ |
| 144 | + func_to_test_1, |
| 145 | + partial(func_to_test_1, 0.12), |
| 146 | + partial(func_to_test_1, sleep_time=0.11), |
| 147 | +]) |
| 148 | + |
| 149 | +``` |
| 150 | + |
| 151 | + |
| 152 | +```python |
| 153 | +bench |
| 154 | +``` |
| 155 | +<details open> <summary>output</summary> |
| 156 | + <pre>Benchmark(func_to_test_1, func_to_test_1(0.12), func_to_test_1(sleep_time=0.11))</pre> |
| 157 | +</details> |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | +```python |
| 163 | +bench.run() |
| 164 | +``` |
| 165 | +<details open> <summary>output</summary> |
| 166 | + <pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"> Func name | Sec <span style="color: #800080; text-decoration-color: #800080">/</span> run |
| 167 | +</pre> |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | +<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">func_to_test_1: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.10</span> <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.0</span>% |
| 173 | +</pre> |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | +<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="color: #800080; text-decoration-color: #800080; font-weight: bold">func_to_test_1</span><span style="font-weight: bold">(</span><span style="color: #808000; text-decoration-color: #808000">sleep_time</span>=<span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.11</span><span style="font-weight: bold">)</span>: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.11</span> <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">-8.9</span>% |
| 179 | +</pre> |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | + |
| 184 | +<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="color: #800080; text-decoration-color: #800080; font-weight: bold">func_to_test_1</span><span style="font-weight: bold">(</span><span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.12</span><span style="font-weight: bold">)</span>: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.12</span> <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">-16.5</span>% |
| 185 | +</pre> |
| 186 | + |
| 187 | +</details> |
| 188 | + |
| 189 | + |
| 190 | + |
| 191 | +```python |
| 192 | +bench = Benchmark({ |
| 193 | + "func_1": func_to_test_1, |
| 194 | + "func_2": func_to_test_2, |
| 195 | +}) |
| 196 | +``` |
| 197 | + |
| 198 | + |
| 199 | +```python |
| 200 | +bench |
| 201 | +``` |
| 202 | +<details open> <summary>output</summary> |
| 203 | + <pre>Benchmark(func_1, func_2)</pre> |
| 204 | +</details> |
| 205 | + |
| 206 | + |
0 commit comments