Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the most efficient way to sleep in Python?

What would be better ?

time.sleep(delayTime)

or

select.select([],[],[],delayTime)

Are they equivalent ? Is select more efficient?

like image 994
edgarstack Avatar asked Jul 05 '16 13:07

edgarstack


People also ask

How do I sleep with Python?

Python has built-in support for putting your program to sleep. The time module has a function sleep() that you can use to suspend execution of the calling thread for however many seconds you specify.

What can I use instead of time to sleep in Python?

The Timer is another method available with Threading, and it helps to get the same functionality as Python time sleep. The working of the Timer is shown in the example below: Example: A Timer takes in input as the delay time in Python in seconds, along with a task that needs to be started.

How many hours a Python can sleep?

Sleeping time of python is three times six, which is equal to 18 hours. Awake time of python is the unshaded portion, which contains one quarter of the circle. So awake time 6×1=6 hours.

How do you sleep 3 seconds in Python?

Python 3 - time sleep() MethodThe method sleep() suspends execution for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep time.


1 Answers

Pretty simple to hackup a test in python with called to timeit, but I love ipython for quick tests (http://ipython.org/). Here's my results:

$ ipython
import time,select

%timeit time.sleep(0)
1000000 loops, best of 3: 655 ns per loop

%timeit select.select([],[],[],0)
1000000 loops, best of 3: 902 ns per loop

But if you don't have access to ipython, and would prefer native timeit from command line:

$ python -m timeit -s "import time,select" "time.sleep(0)"
1000000 loops, best of 3: 0.583 usec per loop

$ python -m timeit -s "import time,select" "select.select([],[],[],0)"
1000000 loops, best of 3: 0.777 usec per loop
like image 150
user590028 Avatar answered Sep 29 '22 10:09

user590028