@@ -6,7 +6,7 @@ Utils for benchmark - wrapper over python timeit.
6
6
[ ![ PyPI Status] ( https://badge.fury.io/py/benchmark-utils.svg )] ( https://badge.fury.io/py/benchmark-utils )
7
7
[ ![ Tests] ( https://github.com/ayasyrev/benchmark_utils/workflows/Tests/badge.svg )] ( https://github.com/ayasyrev/benchmark_utils/actions?workflow=Tests ) [ ![ Codecov] ( https://codecov.io/gh/ayasyrev/benchmark_utils/branch/main/graph/badge.svg )] ( https://codecov.io/gh/ayasyrev/benchmark_utils )
8
8
9
- Tested on python 3.7 - 3.11
9
+ Tested on python 3.7 - 3.12
10
10
11
11
## Install
12
12
@@ -20,12 +20,13 @@ Or install from github repo:
20
20
21
21
## Basic use.
22
22
23
- Lets benchmark some (dump ) functions.
23
+ Lets benchmark some (dummy ) functions.
24
24
25
25
26
26
``` python
27
27
from time import sleep
28
28
29
+
29
30
def func_to_test_1 (sleep_time : float = 0.1 , mult : int = 1 ) -> None :
30
31
""" simple 'sleep' func for test"""
31
32
sleep(sleep_time * mult)
@@ -34,7 +35,6 @@ def func_to_test_1(sleep_time: float = 0.1, mult: int = 1) -> None:
34
35
def func_to_test_2 (sleep_time : float = 0.11 , mult : int = 1 ) -> None :
35
36
""" simple 'sleep' func for test"""
36
37
sleep(sleep_time * mult)
37
-
38
38
```
39
39
40
40
Let's create benchmark.
@@ -56,7 +56,9 @@ bench = Benchmark(
56
56
bench
57
57
```
58
58
<details open > <summary >output</summary >
59
- <pre>Benchmark(func_to_test_1, func_to_test_2)</pre>
59
+ <pre>
60
+ Benchmark(func_to_test_1, func_to_test_2)
61
+ </pre>
60
62
</details >
61
63
62
64
@@ -65,6 +67,7 @@ Now we can benchmark that functions.
65
67
66
68
67
69
``` python
70
+ # we can run bench.run() or just:
68
71
bench()
69
72
```
70
73
<details open > <summary >output</summary >
@@ -80,7 +83,7 @@ bench()
80
83
81
84
82
85
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 >%
86
+ <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.9 </span >%
84
87
</pre >
85
88
86
89
</details >
@@ -105,7 +108,7 @@ bench.run(num_repeats=10)
105
108
106
109
107
110
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 >%
111
+ <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.3 </span >%
109
112
</pre >
110
113
111
114
</details >
@@ -130,30 +133,33 @@ bench.print_results(reverse=True)
130
133
131
134
132
135
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 >%
136
+ <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 " >10.2 </span >%
134
137
</pre >
135
138
136
139
</details >
137
140
138
141
139
- We can add functions to bencmark as list of funtions (or partial) ar as dictionary: ` {"name": function} ` .
142
+ We can add functions to benchmark as list of functions (or partial) or as dictionary: ` {"name": function} ` .
140
143
141
144
142
145
``` 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
-
146
+ bench = Benchmark(
147
+ [
148
+ func_to_test_1,
149
+ partial(func_to_test_1, 0.12 ),
150
+ partial(func_to_test_1, sleep_time = 0.11 ),
151
+ ]
152
+ )
149
153
```
150
154
151
155
152
156
``` python
153
157
bench
154
158
```
155
159
<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>
160
+ <pre>
161
+ Benchmark(func_to_test_1, func_to_test_1(0.12), func_to_test_1(sleep_time=0.11))
162
+ </pre>
157
163
</details >
158
164
159
165
@@ -175,32 +181,132 @@ bench.run()
175
181
176
182
177
183
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 >%
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 : #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.8 </span >%
179
185
</pre >
180
186
181
187
182
188
183
189
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 >%
190
+ <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.4 </span >%
185
191
</pre >
186
192
187
193
</details >
188
194
189
195
190
196
191
197
``` python
192
- bench = Benchmark({
193
- " func_1" : func_to_test_1,
194
- " func_2" : func_to_test_2,
195
- })
198
+ bench = Benchmark(
199
+ {
200
+ " func_1" : func_to_test_1,
201
+ " func_2" : func_to_test_2,
202
+ }
203
+ )
196
204
```
197
205
198
206
199
207
``` python
200
208
bench
201
209
```
202
210
<details open > <summary >output</summary >
203
- <pre>Benchmark(func_1, func_2)</pre>
211
+ <pre>
212
+ Benchmark(func_1, func_2)
213
+ </pre>
214
+ </details >
215
+
216
+
217
+
218
+ When we run benchmark script in terminal, we got pretty progress thanks to rich. Lets run example_1.py from example folder:
219
+
220
+ ![ exaple_1] ( images/run_example_1.gif )
221
+
222
+ # BenchmarkIter
223
+
224
+ With BenchmarkIter we can benchmark functions over iterables, for example read list of files or run functions with different arguments.
225
+
226
+
227
+ ``` python
228
+ def func_to_test_1 (x : int ) -> None :
229
+ """ simple 'sleep' func for test"""
230
+ sleep(0.01 )
231
+
232
+
233
+ def func_to_test_2 (x : int ) -> None :
234
+ """ simple 'sleep' func for test"""
235
+ sleep(0.015 )
236
+
237
+
238
+ dummy_params = list (range (10 ))
239
+ ```
240
+
241
+
242
+ ``` python
243
+ from benchmark_utils import BenchmarkIter
244
+
245
+ bench = BenchmarkIter(
246
+ func = [func_to_test_1, func_to_test_2],
247
+ item_list = dummy_params,
248
+ )
249
+ ```
250
+
251
+
252
+ ``` python
253
+ bench()
254
+ ```
255
+ <details open > <summary >output</summary >
256
+ <pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"> Func name | Items/sec
257
+ </pre >
258
+
259
+ </details >
260
+
261
+
262
+
263
+
264
+
265
+
266
+
267
+
268
+
269
+
270
+ We can run it again, all functions, some of it, exclude some and change number of repeats.
271
+ And we can limit number of items with ` num_samples ` argument:
272
+ ` bench.run(num_samples=5) `
273
+
274
+ # Multiprocessing
275
+
276
+ By default we tun functions in one thread.
277
+ But we can use multiprocessing with ` multiprocessing=True ` argument:
278
+ ` bench.run(multiprocessing=True) `
279
+ It will use all available cpu cores.
280
+ And we can use ` num_workers ` argument to limit used cpu cores:
281
+ ` bench.run(multiprocessing=True, num_workers=2) `
282
+
283
+
284
+ ``` python
285
+ bench.run(multiprocessing = True , num_workers = 2 )
286
+ ```
287
+ <details open > <summary >output</summary >
288
+ <pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"> Func name | Items/sec
289
+ </pre >
290
+
291
+ </details >
292
+
293
+
294
+
295
+
296
+
297
+
298
+
299
+
300
+
301
+
302
+
303
+ ``` python
304
+ 0.1022316165981465 / 0.09319195459829643
305
+ ```
306
+ <details open > <summary >output</summary >
307
+ <pre>
308
+ 1.0970004550158379
309
+ </pre>
204
310
</details >
205
311
206
312
0 commit comments