python - Comparison: import statement vs __import__ function -
as followup question using builtin __import__()
in normal cases, lead few tests, , came across surprising results.
i here comparing execution time of classical import
statement, , call __import__
built-in function. purpose, use following script in interactive mode:
import timeit def test(module): t1 = timeit.timeit("import {}".format(module)) t2 = timeit.timeit("{0} = __import__('{0}')".format(module)) print("import statement: ", t1) print("__import__ function:", t2) print("t(statement) {} t(function)".format("<" if t1 < t2 else ">"))
as in linked question, here comparison when importing sys
, along other standard modules:
>>> test('sys') import statement: 0.319865173171288 __import__ function: 0.38428380458522987 t(statement) < t(function) >>> test('math') import statement: 0.10262547545597034 __import__ function: 0.16307580163101054 t(statement) < t(function) >>> test('os') import statement: 0.10251490255312312 __import__ function: 0.16240755669640627 t(statement) < t(function) >>> test('threading') import statement: 0.11349136644972191 __import__ function: 0.1673617034957573 t(statement) < t(function)
so far good, import
faster __import__()
. makes sense me, because wrote in linked post, find logical import_name
instruction optimized in comparison call_function
, when latter results in call __import__
.
but when comes less standard modules, results reverse:
>>> test('numpy') import statement: 0.18907936340054476 __import__ function: 0.15840019037769792 t(statement) > t(function) >>> test('tkinter') import statement: 0.3798560809537861 __import__ function: 0.15899962771786136 t(statement) > t(function) >>> test("pygame") import statement: 0.6624641952621317 __import__ function: 0.16268579177259568 t(statement) > t(function)
what reason behind difference in execution times? actual reason why import
statement faster on standard modules? on other hand, why __import__
function faster other modules?
tests lead python 3.6
timeit
measures total execution time, first import of module, whether through import
or __import__
, slower subsequent ones - because it's 1 performs module initialization. has search filesystem module's file(s), load module's source code (slowest) or created bytecode (slow bit faster parsing .py
files) or shared library (for c extensions), execute initialization code, , store module object in sys.modules
. subsequent imports skip , retrieve module object sys.modules
.
if reverse order results different:
import timeit def test(module): t2 = timeit.timeit("{0} = __import__('{0}')".format(module)) t1 = timeit.timeit("import {}".format(module)) print("import statement: ", t1) print("__import__ function:", t2) print("t(statement) {} t(function)".format("<" if t1 < t2 else ">")) test('numpy') import statement: 0.4611093703134608 __import__ function: 1.275512785926014 t(statement) < t(function)
the best way non-biased results import once , timings:
import timeit def test(module): exec("import {}".format(module)) t2 = timeit.timeit("{0} = __import__('{0}')".format(module)) t1 = timeit.timeit("import {}".format(module)) print("import statement: ", t1) print("__import__ function:", t2) print("t(statement) {} t(function)".format("<" if t1 < t2 else ">")) test('numpy') import statement: 0.4826306561727307 __import__ function: 0.9192819125911029 t(statement) < t(function)
so, yes, import
faster __import__
.
Comments
Post a Comment