]> granicus.if.org Git - python/commitdiff
Close #20275: Optimize BaseEventLoop._run_once()
authorVictor Stinner <victor.stinner@gmail.com>
Mon, 20 Jan 2014 22:56:40 +0000 (23:56 +0100)
committerVictor Stinner <victor.stinner@gmail.com>
Mon, 20 Jan 2014 22:56:40 +0000 (23:56 +0100)
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.

Lib/asyncio/base_events.py

index a88506561df096843f6e19bbf3a97f766faefc7c..07d49c5e4dceaafe7aea717409b4a302673ce299 100644 (file)
@@ -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.