-
-
Notifications
You must be signed in to change notification settings - Fork 31k
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
time.sleep(0)
is slower on Python 3.11 than on Python 3.10
#125997
Comments
A while ago did some benchmarking on 3.11 and 3.12 with Was expecting nano-seconds, but was in micro-second order. Just assumed that this is how it should be. |
I wasn't able to reproduce this on my end:
Though,
Unfortunately, 3.11 is security-only, so there's nothing that can get fixed here unless this is somehow causing a security issue somewhere. |
Can not reproduce on OSX either: python -m timeit -s 'import time' '[time.sleep(0) for _ in range(1_000_000)]'
# 3.10: 1 loop, best of 5: 631 msec per loop
# 3.11: 1 loop, best of 5: 550 msec per loop
# 3.12: 1 loop, best of 5: 556 msec per loop |
hmm -- fwiw, here is my CPU info (from
i am reproducing this in python3.13 as well. ~ $ python3.13 -VV
Python 3.13.0 (main, Oct 8 2024, 08:51:28) [GCC 11.4.0]
~ $ time python3.13 relax.py
real 0m52.886s
user 0m1.305s
sys 0m1.023s and using ~ $ python3.10 -m timeit -s 'import time' 'time.sleep(0)'
500000 loops, best of 5: 404 nsec per loop
~ $ python3.11 -m timeit -s 'import time' 'time.sleep(0)'
5000 loops, best of 5: 51.9 usec per loop
~ $ python3.12 -m timeit -s 'import time' 'time.sleep(0)'
5000 loops, best of 5: 51.9 usec per loop
~ $ python3.13 -m timeit -s 'import time' 'time.sleep(0)'
5000 loops, best of 5: 51.9 usec per loop |
i am reproducing on another linux machine, too - this time with ubuntu 24.04 $ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 24.04.1 LTS
Release: 24.04
Codename: noble
$ python -m timeit -s 'import time' 'time.sleep(0)'
5000 loops, best of 5: 51.8 usec per loop |
I can reproduce on Linux (but not on macOS) — sleep goes from comfortably under 1us to >50us I'm assuming this is caused by this change: #65501 (it's also mentioned in the docs for
The change is present in Python 3.11 onwards, so it's possible we could change something here. That said, unless someone has a concrete change to suggest, this issue probably isn't going to go anywhere. |
Oh, I do see that in the original report now. I thought this only affected 3.11, my bad! But yeah, I doubt there's anything that can be done here. Maybe this is somehow an upstream Linux problem. |
@hauntsaninja what is the connection with that change? is it that linux defaults to |
just an update -- i can reproduce 3.10 behavior with the following code: #!/usr/bin/env python
import select
#import time # 259ms
def main():
poll = select.poll()
for _ in range(1_000_000):
#time.sleep(0.0)
#select.select([],[],[],0)
poll.poll(0)
main() # 408ms |
How about specializing the sleep when the time is 0? we could just switch to |
time.sleep(0)
regression in 3.11 onwards
This comment has been minimized.
This comment has been minimized.
I ran some benchmarks on Linux (Fedora 41, Linux kernel 6.12.6). On Python 3.10, sleep(0) took 289 ns, whereas it takes 52.6 us on Python 3.11: Python 3.11 is around 182x slower than Python 3.10.
Python 3.11 was modified to use nanosleep() / clock_nanosleep() which explains this difference. Note that a sleep of 1 nanosecond takes exactly 52.6 us on Python 3.10 and 311:
|
FreeBSD doesn't have this issue, even if Python 3.11 implements
Moreover, Python 3.11 is 1.21x faster than Python 3.10. For a sleep of 1 nanosecond, Python 3.11 is 25.5x faster than Python 3.10:
|
StackOverflow answers: |
|
An alternative would be to implement |
time.sleep(0)
regression in 3.11 onwardstime.sleep(0)
is slower on Python 3.11 than on Python 3.10
As a caller, I would expect:
What I however think is that Note that using |
- Add tests for durations of invalid types. - Add tests for `int` and `float` durations, including signed zeroes durations. - Add tests for nonzero very small durations and durations close to the clock resolution. --------- Co-authored-by: Victor Stinner <vstinner@python.org>
…128751) - Add tests for durations of invalid types. - Add tests for `int` and `float` durations, including signed zeroes durations. - Add tests for nonzero very small durations and durations close to the clock resolution. --------- (cherry picked from commit b70a567) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Victor Stinner <vstinner@python.org>
…128751) - Add tests for durations of invalid types. - Add tests for `int` and `float` durations, including signed zeroes durations. - Add tests for nonzero very small durations and durations close to the clock resolution. --------- (cherry picked from commit b70a567) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Victor Stinner <vstinner@python.org>
… (#128795) gh-125997: Increase test coverage for `time.sleep()` (GH-128751) - Add tests for durations of invalid types. - Add tests for `int` and `float` durations, including signed zeroes durations. - Add tests for nonzero very small durations and durations close to the clock resolution. --------- (cherry picked from commit b70a567) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Victor Stinner <vstinner@python.org>
… (#128796) gh-125997: Increase test coverage for `time.sleep()` (GH-128751) - Add tests for durations of invalid types. - Add tests for `int` and `float` durations, including signed zeroes durations. - Add tests for nonzero very small durations and durations close to the clock resolution. --------- (cherry picked from commit b70a567) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Victor Stinner <vstinner@python.org>
…ythonGH-128752) (cherry picked from commit f4afaa6) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Since the behaviour of Sorry for the inconvenience, but I believe that the fact that Therefore, I will consider this issue to be resolved, albeit not with the desired outcome but probably the best outcome from a standard library point of view (also, the fact that this was only reported recently seems to indicate that this regression likely passed unnoticed). |
Bug report
Bug description:
the following script runs 100x slower on python 3.11 and python 3.12:
here is my system+python information:
CPython versions tested on:
3.10, 3.11, 3.12, 3.13
Operating systems tested on:
Linux
Linked PRs
time.sleep(0)
is not delayed on non-Windows platforms #128274 (abandoned proposal)time.sleep()
#128751time.sleep()
(GH-128751) #128795time.sleep()
(GH-128751) #128796time.sleep(0)
#128752time.sleep(0)
(GH-128752) #128984time.sleep(0)
(GH-128752) #128985Resolution: #125997 (comment)
The text was updated successfully, but these errors were encountered: