• Arnaud Fontaine's avatar
    ZODB Components: Execute code in two steps: compile(), then 'exec' instead of only 'exec'. · 6b03c39e
    Arnaud Fontaine authored
    This is much faster, eg defining a max function is about 39 times faster and
    even more on more complicated code:
    
    $ python -mtimeit -s 'code = "def max(a, b): return a > b and a or b"' 'exec code'
    10000 loops, best of 3: 25.2 usec per loop
    $ python -mtimeit -s 'code = compile("def max(a, b): return a > b and a or b", "<string>", "exec")' 'exec code'
    1000000 loops, best of 3: 0.632 usec per loop
    
    Another reason is for traceback/pdb readability as __file__ is displayed in
    the backtrace, but with 'exec' __file__ equals to '<string>', compile() allows
    to specify a readable name easily without having to override __file__.
    
    Also, set __file__ to the Component relative URL instead of the ID for
    readability sake.
    6b03c39e
component_package.py 17 KB