What would be better ?
time.sleep(delayTime)
or
select.select([],[],[],delayTime)
Are they equivalent ? Is select more efficient?
The answer depends on what your trying to achieve:
time.sleep(delayTime)
select.select([],[],[],delayTime)
This is a straightforward interface to the Unix select()
system call. The first three arguments are sequences of 'waitable objects':
So now, that we understand the two interfaces we can understand that the answer depends on the purpose:
If all you want to do is to suspend the current thread - the first option is simpler. But if there are objects to wait on - use the second method. In temp of efficiency - I don't think there are differences if all you are looking for is the simplest use-case (just suspend the main thread).
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
How you defining the efficiency? In most of the cases, the sleep and select have been using to observe if there space/buffer. If the space is not available, then we have option to wait and see when the buffer will be empty and we can execute our action. The sleep() internally select(). So, I think it matters with which you are comfortable with, I guess.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.