On Wed, Jun 20, 2012 at 09:20:04AM +0300, Nadav Har'El wrote: > On Wed, Jun 20, 2012, Meir Kriheli wrote about "Re: Python question - first > call is slower?": > > > I considered, and discredited, the following attempted "explanations": > >... > > > its code gets done in 6 milliseconds; It's not a 12 millisecond pause > > > and then the rest of the function finishes in 2ms. > > > > > > > Yeah, compiles to bytecode. > > As far as I understand, the code is "compiled to bytecode" when it is > imported, *not* when it is first run, so it doesn't explain why the > first run of a function is slower. If you think it's otherwise, please > let me know. > > > Nitpick: CPython doesn't (the one you're referring to as Python), other > > implementations may and will (PyPy, Jython, etc) > > Yes, I'm talking about the "python" executable, which I guess is > CPython. > > > > 3. "If class A imports B which imports C which imports D, some of these > > > classes are only read when the code is actually used for the first > > > time". Again, I couldn't find any evidence that this is true in Python > > > (unlike, e.g., Java). An "import" would read the whole class hierarchy > > > into > > > memory. Right? > > A module is loaded only once (see also: sys.modules) > > Yes, this I know. But at first I thought maybe it is loaded lazily > somehow, so some things are loaded only when a function from the module > is actually run. But I can find no evidence of that - every bit of > documentation I can find suggests that the module, and every module it > refers to, recursively, are fully loaded at the time of the "import" of > the top module. >
I think Meir's point is that if your code eventually calls an stdlib function like def f(): import foo return foo.bar() the first call to the function would require a tedious load of foo.py, which would be avoided on the second call. I guess that `strace -eopen python` would reveal plenty of open(2)s during the first computation. Dan. _______________________________________________ Linux-il mailing list Linux-il@cs.huji.ac.il http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il