Is __import__ known to be slow in windows?

Paul McGuire ptmcg at austin.rr.com
Fri Nov 30 18:45:02 EST 2007


On Nov 30, 5:08 pm, Joshua Kugler <jkug... at bigfoot.com> wrote:
> [I tried googling for this, didn't find anything relevant.]
>
> We've recently been doing some profiling on a project of ours.  It runs
> quite fast on Linux but *really* bogs down on Windows 2003.  We initially
> thought it was the simplejson libraries (we don't use the C extensions) but
> profiling proved otherwise.
>
> We have a function that does some runtime imports via calls to __import__.
> We ran 1000 iterations (we used cProfile) of the application (web app).
> There were eight calls to __import__ per iteration, so 8000 calls total.
> Identical hardware, by the way.
>
> On Linux (Debian Etch, Python 2.5.1)
> Total time was 2.793 CPU seconds, with __import__ using 1.059 seconds of
> that.  So, 37% of the time was spent in import.  Not great, but not a show
> stopper.
>
> On Windows 2003 (R2, Python 2.5.1)
> Total time was 18.532 CPU seconds, with __import__ using 16.330 seconds
> (88%) of that.
>
> So, Linux spends 1.734 seconds on non-import activities, and Windows spends
> 2.202 seconds on non-import activities.  Pretty close.  But 16.3 seconds on
> import!?
>
> Is this a known deficiency in Windows' Python import calls, or is there
> something deeper going on here?
>
> Pointers, suggestions, and URLs welcome.
>
> j

Imagine you have two laundry baskets, one is green, one is purple.
Both contain 10 pairs of pants, but the pockets of the pants in the
purple basket are filled with rocks.

You pick up the green basket - kind of heavy, but not terrible.  Then
you pick up the purple basked - wow!  Really heavy!

Who would have had any idea that the color of the laundry basket would
make such a difference in the weight? :)

Of course, to clear up this question, you empty both baskets, try to
lift each one, and then find out that they both weigh about the same.
(Or one does weigh more than the other, but now you have ruled out the
possibility that the baskets' contents were a factor in the
comparison.)

Ok, back to your question.  __import__ doesn't just load up a module.
At import time, all of the top-level code in the module is executed
also.  So if you want to really assess the impact of *just* __import__
on different platforms, then you should try importing:
- empty .py modules
- .py modules containing no top-level executable statements, but some
class or method definitions
- modules that have already been imported

It's possible that your imported module imports additional modules
which have significant platform-dependent logic, or import modules
which are compiled builtins on one platform, but written in Python on
the other.

As it stands, your experiment still has too many unknowns to draw any
decent conclusion, and it certainly is too early to jump to "wow!
__import__ on Windows is sure slower than __import__ on Linux!"

-- Paul



More information about the Python-list mailing list