From: Victor Stinner Date: Mon, 20 Jan 2014 22:56:40 +0000 (+0100) Subject: Close #20275: Optimize BaseEventLoop._run_once() X-Git-Tag: v3.4.0b3~82 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=22463aa947a75ae3298bca6457f0ae5b738b0d07;p=thirdparty%2FPython%2Fcpython.git 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. --- diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py index a88506561df0..07d49c5e4dce 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.