Flippy Flippy - 1 year ago 64
Python Question

Import Module Benchmark

I have been doing some performance testing and got rather curious at my latest findings.

>>> timeit("import timeit")
>>> timeit("from timeit import timeit")

How can importing the whole module be faster, than importing just a specific part ?

Based on the answers, I have been doing some research and I came across the following:

>>> timeit("x = timeit.timeit", setup="import timeit")
>>> timeit("x = timeit", setup="from timeit import timeit")

Regarding performance, if you plan on using a class/function/submodule a lot, it takes less time if you specify where to import from and can offset or even make up for the time lost in the import.

Answer Source

Because when you want to import one/some part of a module, all the searching through the module's namespace, storing the object in stack and pop from it take time, while for importing the module at once python just does one step, binding the module to its name.

For a better demonstration you can use dis module for checking the bytecode for two recipes separately:

In [10]: def import_all():
    import timeit

In [11]: def import_one():
   ....:     from timeit import timeit

In [12]: import dis

In [13]: dis.dis(import_all)
  2           0 LOAD_CONST               1 (0)
              3 LOAD_CONST               0 (None)
              6 IMPORT_NAME              0 (timeit)
              9 STORE_FAST               0 (timeit)
             12 LOAD_CONST               0 (None)
             15 RETURN_VALUE

In [14]: dis.dis(import_one)
  2           0 LOAD_CONST               1 (0)
              3 LOAD_CONST               2 (('timeit',))
              6 IMPORT_NAME              0 (timeit)
              9 IMPORT_FROM              0 (timeit)
             12 STORE_FAST               0 (timeit)
             15 POP_TOP
             16 LOAD_CONST               0 (None)
             19 RETURN_VALUE

As you can see in second case we have an IMPORT_FROM and POP_TOP more than the first one.