-
Notifications
You must be signed in to change notification settings - Fork 695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Minor improvments to busy_wait #447
base: main
Are you sure you want to change the base?
Conversation
# TODO(rcadene): find an alternative: from python 11, time.sleep is precise | ||
end_time = time.perf_counter() + seconds | ||
while time.perf_counter() < end_time: | ||
def precise_sleep(seconds: float, blocking: bool = False): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if this should default to blocking = True
, and have the fancy non-cpu-blocking version for advanced users.
""" | ||
end_time = time.perf_counter() + seconds | ||
# 20 ms buffer is usually enough to account for an overly-long last sleep, even with heavy system load. | ||
end_time_with_buffer = end_time - 0.02 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried increasing this buffer till I saw clear advantage in using precise_sleep
instead of time.sleep
with maximal system load. Please check on your end.
What this does
Makes
busy_wait
more precise for Linux and non-blocking on other platforms.Renames
busy_wait
toprecise_sleep
, as it's not actually a "busy" wait.Adds a benchmarking script to the
if __name__ = "__main__"
(I know this is unconventional, but it feels convenient and useful enough to break the rules for).How it was tested
Ran the benchmarking snippet with all cores busy and found that
precise_sleep
was better.How to checkout & try? (for the reviewer)
See the benchmarking snippet in the code.