[issue22557] Local import is too slow

Serhiy Storchaka report at bugs.python.org
Sun Jun 19 08:01:09 EDT 2016


Serhiy Storchaka added the comment:

Fixed bugs making test_importlib failing.

Microbenchmark results on faster machine:

$ ./python -m timeit 'import locale'
Unpatched:  1000000 loops, best of 3: 0.839 usec per loop
Patched:    10000000 loops, best of 3: 0.176 usec per loop

$ ./python -m timeit 'import os.path'
Unpatched:  100000 loops, best of 3: 2.02 usec per loop
Patched:    1000000 loops, best of 3: 1.77 usec per loop

$ ./python -m timeit 'from locale import getlocale'
Unpatched:  100000 loops, best of 3: 3.69 usec per loop
Patched:    100000 loops, best of 3: 3.39 usec per loop

And it looks to me that there is a bug in existing code (opened separate issue27352).

0.839 usec is not very slow by CPython's standards, but is equal to about 50 assignments to local variable, 15 attribute revolvings or 5 simple function calls. If some module is optionally needed in fast function, the overhead of local import can be significant. We can lazily initialize global variable (the second example in msg228561), but this code looks more cumbersome.

----------
stage:  -> patch review
versions: +Python 3.6 -Python 3.5
Added file: http://bugs.python.org/file43471/faster_import_4.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue22557>
_______________________________________


More information about the Python-bugs-list mailing list