I have the following code:
for k in pool:
x = []
y = []
try:
exec(pool[k])
except Exception as e:
...
do_something(x)
do_something_else(y)
where pool[k]
is python code that will eventually append items to x
and y
(that's why I am using exec
instead of eval
).
I have tried already to execute the same code with pypy but for this particular block I don't get much better, that line with exec
is still my bottleneck.
That said, my question is:
Is there a faster alternative to exec
?
If not, do you have any workaround to get some speed up in such a case?
--UPDATE--
To clarify, pool
contains around one million keys, to each key it is associated a script (around 50 line of code). The inputs for the scripts are defined before the for loop
and the outputs generated by a script are stored in x
and y
. So, each script has a line in the code stating x.append(something)
and y.append(something)
. The rest of the program will evaluate the results and score each script. Therefore, I need to loop over each script, execute it and process the results. The scripts are originally stored in different text files. pool
is a dictionary obtained by parsing these files.
P.S. Using the pre-compiled version of the code:
for k in pool.keys():
pool[k] = compile(pool[k], '<string>', 'exec')
I have got 5x speed increase, not much but it is something already. I am experimenting with other solutions...