[New-bugs-announce] [issue22557] Locale import is too slow

Serhiy Storchaka report at bugs.python.org
Sun Oct 5 14:29:01 CEST 2014

New submission from Serhiy Storchaka:

Locale import is too slow in comparison with caching module in a global or even in sys.modules.

>>> import timeit
>>> def f():
...     import locale
>>> min(timeit.repeat(f, number=100000, repeat=10))
>>> _locale = None
>>> def g():
...     global _locale
...     if _locale is None:
...         import _locale
>>> min(timeit.repeat(g, number=100000, repeat=10))
>>> import sys
>>> def h():
...     try:
...         locale = sys.modules['locale']
...     except KeyError:
...         import locale
>>> min(timeit.repeat(h, number=100000, repeat=10))

I think there is an overhead of look up __import__, packing arguments in a tuple and calling a function. This can be omitted by looking first in sys.module and calling __import__ only when nothing was found.

components: Interpreter Core
messages: 228561
nosy: brett.cannon, eric.snow, ncoghlan, pitrou, serhiy.storchaka
priority: normal
severity: normal
status: open
title: Locale import is too slow
type: performance
versions: Python 3.5

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list