[New-bugs-announce] [issue27937] logging.getLevelName microoptimization

Ondřej Medek report at bugs.python.org
Fri Sep 2 04:33:08 EDT 2016

New submission from Ondřej Medek:

The logging.getLevelName contains code:

return _levelToName.get(level, _nameToLevel.get(level, ("Level %s" % level)))

I am still a Python beginner, but I think the most of the times the _nameToLevel.get is called unnecessarily. IMHO the code should be

return _levelToName.get(level, (_nameToLevel.get(level, ("Level %s" % level))))

or maybe better a classic if then style:
result = _levelToName.get(level)
if result is None:
   result = _nameToLevel.get(level, ("Level %s" % level))
return result

Since this function is called every time the LogRecord is created, I think it should be microoptimized.

components: Library (Lib)
messages: 274210
nosy: Ondřej Medek
priority: normal
severity: normal
status: open
title: logging.getLevelName microoptimization
type: performance
versions: Python 3.5

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list