-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.mojo
More file actions
32 lines (26 loc) · 1013 Bytes
/
Copy pathutils.mojo
File metadata and controls
32 lines (26 loc) · 1013 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
### Timer utility
### A simple RAII / context-manager timer that prints elapsed wall-clock time in nanoseconds.
from std.time import global_perf_counter_ns
# Timer struct used via Python-style `with` blocks.
#
# Records the time at `__enter__` and prints the elapsed duration at `__exit__`.
# The label prefix is a templated `StringSlice` to avoid allocation.
#
# Usage:
# with Timer("My computation: "):
# do_something()
# # Prints: "My computation: 12345000 nanoseconds"
#
@fieldwise_init
struct Timer[origin: Origin, //](ImplicitlyCopyable):
var start_time: UInt64
var prefix: StringSlice[Self.origin]
def __init__(out self, prefix: StringSlice[Self.origin]):
self.start_time = 0
self.prefix = prefix
def __enter__(mut self) -> Self:
self.start_time = global_perf_counter_ns()
return self
def __exit__(mut self):
elapsed_time_ms = global_perf_counter_ns() - self.start_time
print(self.prefix, elapsed_time_ms, "nanoseconds")