@@ -6,7 +6,7 @@ Utils for benchmark - wrapper over python timeit.
66[ ![ PyPI Status] ( https://badge.fury.io/py/benchmark-utils.svg )] ( https://badge.fury.io/py/benchmark-utils )
77[ ![ 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 )
88
9- Tested on python 3.7 - 3.11
9+ Tested on python 3.7 - 3.12
1010
1111## Install
1212
@@ -20,12 +20,13 @@ Or install from github repo:
2020
2121## Basic use.
2222
23- Lets benchmark some (dump ) functions.
23+ Lets benchmark some (dummy ) functions.
2424
2525
2626``` python
2727from time import sleep
2828
29+
2930def func_to_test_1 (sleep_time : float = 0.1 , mult : int = 1 ) -> None :
3031 """ simple 'sleep' func for test"""
3132 sleep(sleep_time * mult)
@@ -34,7 +35,6 @@ def func_to_test_1(sleep_time: float = 0.1, mult: int = 1) -> None:
3435def func_to_test_2 (sleep_time : float = 0.11 , mult : int = 1 ) -> None :
3536 """ simple 'sleep' func for test"""
3637 sleep(sleep_time * mult)
37-
3838```
3939
4040Let's create benchmark.
@@ -56,7 +56,9 @@ bench = Benchmark(
5656bench
5757```
5858<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>
6062</details >
6163
6264
@@ -65,6 +67,7 @@ Now we can benchmark that functions.
6567
6668
6769``` python
70+ # we can run bench.run() or just:
6871bench()
6972```
7073<details open > <summary >output</summary >
@@ -80,7 +83,7 @@ bench()
8083
8184
8285
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 >%
8487</pre >
8588
8689</details >
@@ -105,7 +108,7 @@ bench.run(num_repeats=10)
105108
106109
107110
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 >%
109112</pre >
110113
111114</details >
@@ -130,30 +133,33 @@ bench.print_results(reverse=True)
130133
131134
132135
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 >%
134137</pre >
135138
136139</details >
137140
138141
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} ` .
140143
141144
142145``` 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+ )
149153```
150154
151155
152156``` python
153157bench
154158```
155159<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>
157163</details >
158164
159165
@@ -175,32 +181,132 @@ bench.run()
175181
176182
177183
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 >%
179185</pre >
180186
181187
182188
183189
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 >%
185191</pre >
186192
187193</details >
188194
189195
190196
191197``` 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+ )
196204```
197205
198206
199207``` python
200208bench
201209```
202210<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>
204310</details >
205311
206312
0 commit comments