Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import Module Benchmark

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

>>> timeit("import timeit")
0.8010718822479248
>>> timeit("from timeit import timeit")
1.3421258926391602

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

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

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

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.

like image 370
Frederico Mateus Martins Avatar asked May 15 '26 00:05

Frederico Mateus Martins


2 Answers

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.

like image 74
Mazdak Avatar answered May 17 '26 14:05

Mazdak


import timeit will get the module directly while from timeit import timeit will take time browsing entire timeit module. Hence the results.

like image 24
Techidiot Avatar answered May 17 '26 15:05

Techidiot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!