[Python-Dev] Startup time

Skip Montanaro skip@pobox.com
Fri, 16 May 2003 14:00:24 -0500


    mal> FWIW, I've removed the re usage from encodings/__init__.py.

    mal> Could you check whether this makes a difference in startup time
    mal> now?

Well...  Not really, but it's not your fault.  site.py imports
distutils.util which imports re.  It does a fair amount of regex compiling,
some at the module level, so deferring "import re" may take a couple minutes
of work.  Hang on...

Okay, now re isn't imported.  The only runtime difference between the two
sets of times below is encodings/__init__.py 1.18 vs 1.19.  Each set of
times is for this command:

    % time ./python.exe -c pass

Everything was already byte compiled.  The times reported were the best of
five.  I tried to quiet the system as much as possible.  Still, since the
amount of work being done is minimal, it's tought to get a good feel for any
differences.

    version 1.18 (w/ re)

    real    0m0.143s
    user    0m0.030s
    sys     0m0.060s

    version 1.19 (no re)

    real    0m0.142s
    user    0m0.040s
    sys     0m0.060s

Note the rather conspicuous lack of any difference.  The only modifications
to my Lib tree are these:

    M cgitb.py
    M warnings.py
    M distutils/util.py
    M encodings/__init__.py
    M test/test_bsddb185.py

I verified that site was imported from my Lib tree:

    % ./python.exe -v -c pass 2>&1 | egrep 'site'
    # /Users/skip/src/python/head/dist/src/build.opt/../Lib/site.pyc matches /Users/skip/src/python/head/dist/src/build.opt/../Lib/site.py
    import site # precompiled from /Users/skip/src/python/head/dist/src/build.opt/../Lib/site.pyc
    # cleanup[1] site

It would appear that the encodings stuff isn't getting imported on my
platform (Mac OS X):

    % ./python.exe -v -c pass 2>&1 | egrep 'encoding'
    %

Looking at site.py shows that the encodings package is only imported on
win32 and only if the codecs.lookup() call fails.  Oh well, we don't care
about minority platforms. ;-) More seriously, to test your specific change
someone will have to run the check Windows.

To contribute something maybe positive, here's the same timing comparison
using my changed version of distutils.util vs CVS:

    CVS:

    real    0m0.155s
    user    0m0.050s
    sys     0m0.070s

    Changed (no module-level re import):

    real    0m0.143s
    user    0m0.070s
    sys     0m0.040s

It appears eliminating "import re" has only a very small effect for me.  It
looks like an extra 6 modules are imported (25 vs 19).

Skip