[Python-Dev] Startup time

Jeff Epler jepler@unpythonic.net
Wed, 7 May 2003 14:42:17 -0500


On Wed, May 07, 2003 at 02:05:06PM -0500, Skip Montanaro wrote:
> I don't know if this still holds true, but at one point during the 2.x
> series I think it was pretty expensive to perform imports inside functions,
> much more expensive than in 1.5.2 at least (maybe right after nested scopes
> were introduced?).  If that is still true, moving the import might be false
> economy.

$ ./python Lib/timeit.py -s "def f(): import sys" "f()"
100000 loops, best of 3: 3.34 usec per loop
$ ./python Lib/timeit.py -s "def f(): pass" "import sys; f()"
100000 loops, best of 3: 3.3 usec per loop
$ ./python Lib/timeit.py -s "def f(): pass" "f()"
1000000 loops, best of 3: 0.451 usec per loop
$ ./python Lib/timeit.py 'import sys'
100000 loops, best of 3: 2.88 usec per loop

About 2.8usec would be added to each invocation of the functions in
question, about the same as the cost of a global-scope import.  This
means that you lose overall as soon as the function is called twice.

.. but this was about speeding python startup, not just speeding python.
<.0375 wink>

Jeff