Created
April 16, 2024 17:34
-
-
Save Ragnoroct/b1ea576c2a12801c2b1071bba8f3ba39 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def real_uptime_ns(): | |
from time import CLOCK_BOOTTIME | |
from time import clock_gettime_ns | |
from os import sysconf, sysconf_names | |
with open("/proc/self/stat") as f: | |
stat_fields = f.readline().split() | |
proc_start_since_boot_tick = float(stat_fields[21]) | |
start_since_boot_sec = proc_start_since_boot_tick / sysconf( | |
sysconf_names["SC_CLK_TCK"] | |
) | |
return clock_gettime_ns(CLOCK_BOOTTIME) - int(start_since_boot_sec * 1_000_000_000) | |
def human_ns(time_ns_val): | |
if time_ns_val >= 60_000_000_000: | |
return "{:.1f}m".format(time_ns_val / 60_000_000_000) | |
elif time_ns_val >= 1_000_000_000: | |
return "{:.3f}s".format(time_ns_val / 1_000_000_000) | |
elif time_ns_val >= 1_000_000: | |
return "{:.1f}ms".format(time_ns_val / 1_000_000) | |
else: | |
return "{}ns".format(time_ns_val) | |
if __name__ == "__main__": | |
from time import sleep | |
from sys import stdout | |
import os | |
sleep(0.542) | |
stdout.write("real_uptime_ns: {}\n".format(human_ns(real_uptime_ns()))) | |
getattr(os, "_exit")(0) # exit as soon as possible |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment