Performance in exec environnements

Ian Kelly ian.g.kelly at gmail.com
Tue Jan 13 14:46:09 EST 2015


On Tue, Jan 13, 2015 at 10:02 AM, Jean-Baptiste Braun
<jbaptiste.braun at gmail.com> wrote:
> Hi,
>
> I'm working on auto-generated python code with the exec function. I've done
> some performance benches :
>
> % python -m timeit '1 + 1'
> 10000000 loops, best of 3: 0.0229 usec per loop
>
> % python -m timeit "exec('1 + 1')"
> 100000 loops, best of 3: 11.6 usec per loop
>
> -> Maybe creating an exec environnement (I don't know how it works) takes
> time. But :
>
> % python -m timeit "1 + 1; 1 + 1"
> 10000000 loops, best of 3: 0.042 usec per loop
>
> % python -m timeit "exec('1 + 1; 1 + 1')"
> 100000 loops, best of 3: 15.7 usec per loop
>
> -> As if executing one more 1 + 1 would take 4 more seconds (100000
> iterations) in an exec environnement.
>
> Am I missing something or should I expect that result ? What does using exec
> imply that causes such a difference ?

Since you're execing a string, the string has to be compiled each time
exec is called. In contrast, the 1 + 1 in the timeit loop is only
compiled once.

C:\Users\Ian>python -m timeit "1 + 1"
10000000 loops, best of 3: 0.0397 usec per loop

C:\Users\Ian>python -m timeit "exec('1 + 1')"
100000 loops, best of 3: 17 usec per loop

C:\Users\Ian>python -m timeit "compile('1 + 1', '', 'exec')"
100000 loops, best of 3: 17 usec per loop

C:\Users\Ian>python -m timeit -s "c = compile('1 + 1', '', 'exec')" "exec(c)"
1000000 loops, best of 3: 0.684 usec per loop

I expect the function call probably accounts for most of the rest of
the difference.



More information about the Python-list mailing list