Slowness in compile()/eval() (summary)

Pedro Rodriguez pedro_rodriguez at club-internet.fr
Fri Jan 18 15:35:38 CET 2002


Thank Alex, I think this summers up what I understand and I think
it gives a better understanding of the way Python works :

- compile()
  compiles without making any assuption of what local variables
  exists, so it uses the LOAD_NAME call to get variables. This
  byte code will do the look up in the local and global namespaces.
  [I put aside things related to LOAD_DEREF from nested scopes]

- functions
  in functions, parameters and local variables are accessed with
  LOAD_FAST byte code which is faster than the LOAD_NAME.

- there is a bug is Python 1.5.2
    def f():
        a = None
        code = compile("a=1", '<string>', 'exec')
        exec code
        print a

    f()

  prints None in 1.5.2, but 1 in python 2.x

- the remark about mixin compile with 'exec' and eval() on resulting
  code :

    compile with     execute with exec   execute with eval
      exec             OK                  OK (*)
      eval             OK                  OK
      single           OK                  OK

  * : this one looks dubious to me, but is it a really a bug

----------------------------------------------------------------------
test code for exec/eval mixes
----------------------------------------------------------------------
print "-"
e = "len(e)"
code = compile(e, '<string>', 'exec')
eval(code)
exec code

print "-"
e = "x=len(e)"
code = compile(e, '<string>', 'exec')
eval(code)
exec code

print "-"
e = "len(e)"
code = compile(e, '<string>', 'single')
eval(code)
exec code

print "-"
e = "x=len(e)"
code = compile(e, '<string>', 'single')
eval(code)
exec code
----------------------------------------------------------------------

-- 

Pedro



More information about the Python-list mailing list