Optimize Code
Module creates unique variables each time it is executed. By writing a function using Block and globally unique variables, repeated execution is sped up because the variable-creation step is omitted.
Remove Module in favor of Block with globally unique variables.
| In[1]:= | ![]() X |
Create an inactive function.
| In[2]:= | ![]() X |
| Out[2]= |
Apply the transformation.
| In[3]:= | X |
| Out[3]= |
Activate the code and the transformed code to make definitions for fModule and fBlock.
| In[4]:= | X |
Compare values for random test values.
| In[5]:= | X |
| Out[5]= |
Comparing timings for a large set of test values shows a factor of 2.5 speed improvement.
| In[6]:= | ![]() X |
| Out[6]= |


