summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2014-01-20 22:56:40 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2014-01-20 22:56:40 (GMT)
commit22463aa947a75ae3298bca6457f0ae5b738b0d07 (patch)
tree6be94e80ebc26018efc8b43290e223585bb1499c
parentc46d1faa4a4b483f2b241bfbe932824d54f2f026 (diff)
downloadcpython-22463aa947a75ae3298bca6457f0ae5b738b0d07.zip
cpython-22463aa947a75ae3298bca6457f0ae5b738b0d07.tar.gz
cpython-22463aa947a75ae3298bca6457f0ae5b738b0d07.tar.bz2
Close #20275: Optimize BaseEventLoop._run_once()
Logger.log() is "slow", logger.isEnabledFor() is faster and the logger is disabled in most cases. A microbenchmark executing 100,000 dummy tasks is 22% faster with this change.
-rw-r--r--Lib/asyncio/base_events.py19
1 files changed, 11 insertions, 8 deletions
diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py
index a885065..07d49c5 100644
--- a/Lib/asyncio/base_events.py
+++ b/Lib/asyncio/base_events.py
@@ -610,15 +610,18 @@ class BaseEventLoop(events.AbstractEventLoop):
timeout = min(timeout, deadline)
# TODO: Instrumentation only in debug mode?
- t0 = self.time()
- event_list = self._selector.select(timeout)
- t1 = self.time()
- argstr = '' if timeout is None else ' {:.3f}'.format(timeout)
- if t1-t0 >= 1:
- level = logging.INFO
+ if logger.isEnabledFor(logging.INFO):
+ t0 = self.time()
+ event_list = self._selector.select(timeout)
+ t1 = self.time()
+ argstr = '' if timeout is None else ' {:.3f}'.format(timeout)
+ if t1-t0 >= 1:
+ level = logging.INFO
+ else:
+ level = logging.DEBUG
+ logger.log(level, 'poll%s took %.3f seconds', argstr, t1-t0)
else:
- level = logging.DEBUG
- logger.log(level, 'poll%s took %.3f seconds', argstr, t1-t0)
+ event_list = self._selector.select(timeout)
self._process_events(event_list)
# Handle 'later' callbacks that are ready.