问题
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?
回答1:
- Using eval here will certainly be slower than a non-eval version, but most likely it won't be a bottleneck in you code. However, the performance is only one issue, maintenance (incl. debugging), as well as readability are other ones.
- The slowdown occurs because Matlab uses a JIT compiler, and
eval
lines cannot be optimized. - Eval use is in most cases due to lack of knowledge about the Matlab functionality that would be appropriate instead. In this particular case, the issue is that the figure handles are stored in variable names called
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.
回答2:
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 array fig(1)
, fig(2)
etc and write
for n = 1:4
filename = sprintf('sName%d', n);
saveas(fig(n), filename, 'fig');
end
which 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 use eval
).
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.
回答3:
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:
- Accessing property names in the ignorance of
[]
notation - Calling functions based on an argument name, which could instead be done with a
switch
(safer, prevents risk of code injection) - Accessing variables named like
var1
,var2
,var3
when they should be arrays instead
To 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 to goto
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.
回答4:
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:
foo()
But this one needs a pragma added:
%#function foo
eval('foo()')
Otherwise you will encounter a runtime problem.
来源:https://stackoverflow.com/questions/10272522/use-and-implications-of-evalexpression-in-matlab-code