Why is Python so slow ?- revisited.

Thomas Wouters thomas at xs4all.nl
Sat Jun 24 11:28:42 EDT 2000


On Fri, 23 Jun 2000 03:50:13 GMT, Andy Freeman <anamax at earthlink.net> wrote:
>In article <m34s6l4flf.fsf at atrus.jesus.cam.ac.uk>,
>  Michael Hudson <mwh21 at cam.ac.uk> wrote:
>> Certainly at some point there was no LOAD_FAST opcode; every name
>> lookup would first look into a local dict, then the global one, then
>> the builtins.  Now references to locals are just indexes into (C)
>> vectors; this is much quicker.

>Is there a standard way to turn off this optimization on a def
>by def basis?

Not really, and it shouldn't be necessary, either. A function's locals are
fairly unreachable, so they cannot be modified by something other than the
function itself(*), and the function itself is analyzed at compile time, to
see if it does any self-modifying ('from module import *', and 'exec', to be
precise.) So, you can turn the optimization 'off' by using something like:

exec "1"

somewhere in your function (though not in an 'unreachable' branch, like in a
'if 0:' -- that'll get optimized away.)

But because a function's locals are so unreachable, there should really be
no need to turn off the optimization. (Which is also why it's default on,
instead of dependant on the '-O' or '-OO' switch, or the like.

(*) It might be possible to change a functions' locals using the
bytecodehacks package, but I've never looked at that package before, so I
wouldn't know.

Newsgroup-instead-of-list-for-the-first-time-ly yr's,
Thomas.



More information about the Python-list mailing list