[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))
0.4501200000013341
>>> _locale = None
>>> def g():
...     global _locale
...     if _locale is None:
...         import _locale
... 
>>> min(timeit.repeat(g, number=100000, repeat=10))
0.07821200000034878
>>> import sys
>>> def h():
...     try:
...         locale = sys.modules['locale']
...     except KeyError:
...         import locale
... 
>>> min(timeit.repeat(h, number=100000, repeat=10))
0.12357599999813829

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>
<http://bugs.python.org/issue22557>
_______________________________________


More information about the Python-bugs-list mailing list