Message 228561 - Python tracker

This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Content
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.