-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathperf-ns.py
More file actions
executable file
·49 lines (39 loc) · 1.95 KB
/
perf-ns.py
File metadata and controls
executable file
·49 lines (39 loc) · 1.95 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python3
""" perf-ns.py at https://github.com/wilsonmar/python-samples/blob/main/perf-ns.py
An example of obtaining timings in nanosecond-level resolution.
CURRENT STATUS: WORKING on macOS M2 14.5 (23F79) using Python 3.12.7.
git commit -m"v003 + program level timer :perf-ns.py"
The t_ns_duration is the difference between timestamps obtained by calling
time.perf_counter_ns() which returns a monotonic time that only increases.
However, time.time() can decrease, such as when the system clock is synchronised
with an external NTP server.
"""
# import time
from time import perf_counter_ns
import time # for sleep.
# GLOBALS:
SLEEP_SECS = 0.5
start_time = time.time() # start the timer running.
TASK_NAME = "Sleep"
t_ns_start = perf_counter_ns() # Start task time stamp
# Do something that takes time: Replace Sleep with actual work:
time.sleep(SLEEP_SECS) # seconds
t_ns_stop = time.perf_counter_ns() # Stop time stamp
# The difference between both time stamps is the t_ns_duration:
t_ns_duration_ns = (t_ns_stop - t_ns_start) # naonseconds (ns)
t_ns_duration_µs = t_ns_duration_ns / 1000 # microseconds (µs)
t_ns_duration_ms = t_ns_duration_µs / 1000 # milliseconds (ms)
t_ns_duration_secs = t_ns_duration_ms / 1000 # seconds (secs)
t_ns_duration_mins = t_ns_duration_secs / 1000 # minutes (mins)
print(f"*** PERF: Task \"{TASK_NAME}\" took: {t_ns_duration_secs:,.3f} secs"
f", {t_ns_duration_ms:,.2f} millisecs-ms"
f", {t_ns_duration_µs:,.1f} microsecsonds-µs"
f", {t_ns_duration_ns:,} nano-secs"
)
# PROTIP: Different levels of precision:
# OUTPUT: *** PERF: Task "Sleep" took: 0.505 secs, 504.89 millisecs-ms, 504,886.8 microsecsonds-µs, 504,886,792 nano-secs
# STEP: Calculate the execution time:
end_time = time.time()
execution_time = end_time - start_time
print(f"*** PERF: Program took {execution_time:.4f} seconds to run all tasks.")
# OUTPUT: *** PERF: Program took 0.5052 seconds to run all tasks.