[Python-Dev] Startup time

Alex Martelli aleax@aleax.it
Wed, 7 May 2003 22:57:26 +0200


On Wednesday 07 May 2003 09:05 pm, Skip Montanaro wrote:
>     Jeff> In encodings.__init__.py, the only use of re is for the
>     Jeff> normalize_encoding function.  It could potentially be replaced
> with only Jeff> string operations:
>     ...
>     Jeff> .. or the import of re could be moved inside normalize_encoding.
>
> 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.

Doesn't seem to be true in 2.3, if I understand what you're saying:

[alex@lancelot src]$ python Lib/timeit.py -s'def f(): pass' 'import math; f()'
100000 loops, best of 3: 4.04 usec per loop

[alex@lancelot src]$ python Lib/timeit.py -s'def f(): import math' 'pass; f()'
100000 loops, best of 3: 4.05 usec per loop

or even

[alex@lancelot src]$ python Lib/timeit.py -s'import math' -s'def f(): pass' 
'reload(math); f()'
10000 loops, best of 3: 168 usec per loop

[alex@lancelot src]$ python Lib/timeit.py -s'import math' -s'def f(): 
reload(math)' 'pass; f()'
10000 loops, best of 3: 169 usec per loop


Alex