Why is calling eval so slow?

Serhiy Storchaka storchaka at gmail.com
Sun May 6 00:58:12 EDT 2018


06.05.18 05:29, Steven D'Aprano пише:
> On Sat, 05 May 2018 20:11:28 +0300, Serhiy Storchaka wrote:
> 
>>> Why so slow?
>>
>> 1. Add an overhead of calling eval(), not just looking up its name. It
>> is comparable with a time of calling a simple lambda. 2. Add an overhead
>> of getting the globals dict and creating a locals dict.
> [...]
> 
> Ah, that makes sense.
> 
> So presumably the fastest way to run some code in Python is to put it
> into a function and call the function, rather than playing with eval or
> exec, right?

Right. And advanced implementations, like PyPy can apply optimizations 
in this case.

Maybe putting code into an operator implementation will be even faster 
because of getting rid of the part of the overhead of calling a 
functions. Didn't check this.




More information about the Python-list mailing list