Import Module Benchmark
<p>I have been doing some performance testing and got rather curious at my latest findings.</p>

<pre><code>&gt;&gt;&gt; timeit("import timeit")
0.8010718822479248
&gt;&gt;&gt; timeit("from timeit import timeit")
1.3421258926391602
</code></pre>

<p>How can importing the <strong>whole module be faster</strong>, than importing just a <strong>specific part</strong> ?</p>

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

<pre><code>&gt;&gt;&gt; timeit("x = timeit.timeit", setup="import timeit")
0.09205102920532227
&gt;&gt;&gt; timeit("x = timeit", setup="from timeit import timeit")
0.0244600772857666
</code></pre>

<p>Regarding <strong>performance</strong>, if you plan on using a <em>class/function/submodule</em> 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.</p>

<p>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.</p>

<p>For a better demonstration you can use <code>dis</code> module for checking the bytecode for two recipes separately:</p>

<pre><code>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
</code></pre>

<p>As you can see in second case we have an <code>IMPORT_FROM</code> and <code>POP_TOP</code> more than the first one.</p>

python performance time