I was encountered with usage of function eval(expression) in somebody else's code in matlab: for example:
for n = 1 : 4
sn = int2str( n) ;
eval( [ 'saveas( fig' sn ', [ sName' sn ' ], ''fig'' ) ' ] );
end
MathWorks stuff in Matlab Help pointed out that:
Many common uses of the eval function are less efficient and are more difficult to read and debug than other MATLAB functions and language constructs.
After this, I find usage of this function in many other program languages, so as Python, JavaScript, PHP.
So I have a few questions:
- Will usage of this function be enfluenced on the performance of my code?
- If it will slow down execution, why does it occur?
- If it slow down execution every time when called, what reason for use this function in principle?
Here is another implication:
When you compile a program that uses
eval
, you must put pragmas that tell the compiler that some functions are needed. For example:This code will compile and run well:
But this one needs a pragma added:
Otherwise you will encounter a runtime problem.
eval
lines cannot be optimized.fig1
throughfig4
. If they had been stored in an array calledfig
, i.e.fig(1)
etc,eval
would have been unnecessary.EDIT Here are two excellent articles by Loren Shure about why
eval
should be avoided in Matlab. Evading eval, and More on eval.For the most part, the slowdown occurs because the string has to be parsed into actual code. This isn't such a major issue if used sparingly, but if you ever find yourself using it in code that loops (either an explicit loop or things like JavaScript's
setInterval()
) then you're in for a major performance drop.Common uses I've seen for
eval
that could be done better are:[]
notationswitch
(safer, prevents risk of code injection)var1
,var2
,var3
when they should be arrays insteadTo be honest, I don't think I've ever encountered a single situation where
eval
is the only way to solve a problem. I guess you could liken it togoto
in that it's a substitute for program structure, or useful as a temporary solution to test a program before spending the time making it work optimally.The
eval
function is dangerous and there are very few examples where you actually need it. For example, the code you gave can easily be rewritten if you store the figure handles in an arrayfig(1)
,fig(2)
etc and writewhich is clearer, uses fewer characters, can be analysed by the Matlab editor's linter, is more easily modifiable if (when) you need to extend the code, and is less prone to weird bugs.
As a rule of thumb, you should never use
eval
in any language unless you really know what you are doing (i.e. you are writing a complicated Lisp macro or something else equivalent to manipulating the AST of the language - if you don't know what that means, you probably don't need to useeval
).There are almost always clearer, more efficient and less dangerous ways to achieve the same result. Often, a call to
eval
can be replaced with some form of recursion, a higher-order function or a loop.