nano-dev-utils 0.5.7__py3-none-any.whl → 0.5.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of nano-dev-utils might be problematic. Click here for more details.
- nano_dev_utils/timers.py +35 -24
- {nano_dev_utils-0.5.7.dist-info → nano_dev_utils-0.5.8.dist-info}/METADATA +6 -4
- nano_dev_utils-0.5.8.dist-info/RECORD +8 -0
- nano_dev_utils-0.5.7.dist-info/RECORD +0 -8
- {nano_dev_utils-0.5.7.dist-info → nano_dev_utils-0.5.8.dist-info}/WHEEL +0 -0
- {nano_dev_utils-0.5.7.dist-info → nano_dev_utils-0.5.8.dist-info}/licenses/LICENSE +0 -0
nano_dev_utils/timers.py
CHANGED
|
@@ -12,27 +12,38 @@ class Timer:
|
|
|
12
12
|
self.verbose = verbose
|
|
13
13
|
self.units = [(1e9, 's'), (1e6, 'ms'), (1e3, 'μs'), (1.0, 'ns')]
|
|
14
14
|
|
|
15
|
-
def timeit(
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
15
|
+
def timeit(
|
|
16
|
+
self, iterations: int = 1
|
|
17
|
+
) -> Callable[[Callable[P, R]], Callable[P, R | None]]:
|
|
18
|
+
def decorator(func: Callable[P, R]) -> Callable[P, R | None]:
|
|
19
|
+
"""Decorator that times function execution with automatic unit scaling and averaging."""
|
|
20
|
+
|
|
21
|
+
@wraps(func)
|
|
22
|
+
def wrapper(*args: P.args, **kwargs: P.kwargs) -> R | None:
|
|
23
|
+
total_elapsed = 0
|
|
24
|
+
result = None
|
|
25
|
+
|
|
26
|
+
for _ in range(iterations):
|
|
27
|
+
start = time.perf_counter_ns()
|
|
28
|
+
result = func(*args, **kwargs)
|
|
29
|
+
total_elapsed += time.perf_counter_ns() - start
|
|
30
|
+
|
|
31
|
+
avg_elapsed = total_elapsed / iterations
|
|
32
|
+
value = avg_elapsed
|
|
33
|
+
unit = 'ns'
|
|
34
|
+
|
|
35
|
+
for divisor, unit in self.units:
|
|
36
|
+
if avg_elapsed >= divisor or unit == 'ns':
|
|
37
|
+
value = avg_elapsed / divisor
|
|
38
|
+
break
|
|
39
|
+
|
|
40
|
+
extra_info = f'{args} {kwargs} ' if self.verbose else ''
|
|
41
|
+
iter_info = f' (avg over {iterations} runs)' if iterations > 1 else ''
|
|
42
|
+
print(
|
|
43
|
+
f'{func.__name__} {extra_info}took {value:.{self.precision}f} [{unit}]{iter_info}'
|
|
44
|
+
)
|
|
45
|
+
return result
|
|
46
|
+
|
|
47
|
+
return wrapper
|
|
48
|
+
|
|
49
|
+
return decorator
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: nano_dev_utils
|
|
3
|
-
Version: 0.5.
|
|
3
|
+
Version: 0.5.8
|
|
4
4
|
Summary: A collection of small Python utilities for developers.
|
|
5
5
|
Project-URL: Homepage, https://github.com/yaronday/nano_utils
|
|
6
6
|
Project-URL: Issues, https://github.com/yaronday/nano_utils/issues
|
|
@@ -34,10 +34,12 @@ This module provides a `Timer` class for measuring the execution time of code bl
|
|
|
34
34
|
* `verbose`: Optionally displays the function's positional arguments (args) and keyword arguments (kwargs).
|
|
35
35
|
Defaults to `False`.
|
|
36
36
|
|
|
37
|
-
* **`timeit(
|
|
37
|
+
* **`timeit(
|
|
38
|
+
self, iterations: int = 1
|
|
39
|
+
) -> Callable[[Callable[P, R]], Callable[P, R | None]]`**:
|
|
38
40
|
Decorator that times function execution with automatic unit scaling.
|
|
39
41
|
* When the decorated function is called, this decorator records the start and end times,
|
|
40
|
-
calculates the
|
|
42
|
+
calculates the average execution time, prints the function name and execution
|
|
41
43
|
time (optionally including arguments), and returns the result of the original function.
|
|
42
44
|
|
|
43
45
|
#### Example Usage:
|
|
@@ -49,7 +51,7 @@ from nano_dev_utils.timers import Timer
|
|
|
49
51
|
timer = Timer(precision=6, verbose=True)
|
|
50
52
|
|
|
51
53
|
|
|
52
|
-
@timer.timeit
|
|
54
|
+
@timer.timeit()
|
|
53
55
|
def my_function(a, b=10):
|
|
54
56
|
"""A sample function."""
|
|
55
57
|
time.sleep(0.1)
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
nano_dev_utils/__init__.py,sha256=imiI367TPj5s4IFmi-VrKJLbdkIxdIPISNscthoaS9U,454
|
|
2
|
+
nano_dev_utils/dynamic_importer.py,sha256=cm2VwDYSGwhGZNO3uMX-O0LaKtEFtzkPm7BrZW4igG4,911
|
|
3
|
+
nano_dev_utils/release_ports.py,sha256=sgmoPax9Hpcse1rHbBSnDJWTkvV6aWpZ5hQFxBKhGR8,5886
|
|
4
|
+
nano_dev_utils/timers.py,sha256=IiCltOHRkYqfX_IzHQ2Xty4ptITBiaOldFO1HhQ6r_A,1748
|
|
5
|
+
nano_dev_utils-0.5.8.dist-info/METADATA,sha256=KA_vxHbiStc8At1rFrWu2Rt8G_eJzyrRLeb3tYQWfUs,5550
|
|
6
|
+
nano_dev_utils-0.5.8.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
7
|
+
nano_dev_utils-0.5.8.dist-info/licenses/LICENSE,sha256=Muenl7Bw_LdtHZtlOMAP7Kt97gDCq8WWp2605eDWhHU,1089
|
|
8
|
+
nano_dev_utils-0.5.8.dist-info/RECORD,,
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
nano_dev_utils/__init__.py,sha256=imiI367TPj5s4IFmi-VrKJLbdkIxdIPISNscthoaS9U,454
|
|
2
|
-
nano_dev_utils/dynamic_importer.py,sha256=cm2VwDYSGwhGZNO3uMX-O0LaKtEFtzkPm7BrZW4igG4,911
|
|
3
|
-
nano_dev_utils/release_ports.py,sha256=sgmoPax9Hpcse1rHbBSnDJWTkvV6aWpZ5hQFxBKhGR8,5886
|
|
4
|
-
nano_dev_utils/timers.py,sha256=EJFFU9y5qv9w9mfjtBsrgyCXj7NoBpXH0AzPewFnRa0,1205
|
|
5
|
-
nano_dev_utils-0.5.7.dist-info/METADATA,sha256=NsqOX4UXr2EhEW7qUkLz_5qL95B9PmqTuDLR4NsNkVY,5498
|
|
6
|
-
nano_dev_utils-0.5.7.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
7
|
-
nano_dev_utils-0.5.7.dist-info/licenses/LICENSE,sha256=Muenl7Bw_LdtHZtlOMAP7Kt97gDCq8WWp2605eDWhHU,1089
|
|
8
|
-
nano_dev_utils-0.5.7.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|