Author serhiy.storchaka
Recipients brett.cannon, eric.snow, ncoghlan, pitrou, serhiy.storchaka
Date 2014-10-05.12:29:01
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1412512141.77.0.0512115045124.issue22557@psf.upfronthosting.co.za>
In-reply-to
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.
History
Date User Action Args
2014-10-05 12:29:01serhiy.storchakasetrecipients: + serhiy.storchaka, brett.cannon, ncoghlan, pitrou, eric.snow
2014-10-05 12:29:01serhiy.storchakasetmessageid: <1412512141.77.0.0512115045124.issue22557@psf.upfronthosting.co.za>
2014-10-05 12:29:01serhiy.storchakalinkissue22557 messages
2014-10-05 12:29:01serhiy.storchakacreate