gpt4 book ai didi

matlab - 为什么 `eval` 比 `str2func` 从字符串评估函数更差?

转载 作者:太空宇宙 更新时间:2023-11-03 20:09:19 26 4
gpt4 key购买 nike

<分区>

我已经证明了 performance of str2func is better , 但我收到了很多评论,指出有更基本的 reasons不使用 eval。哪些根本原因适用于 eval不适用于 str2func在以下情况下:

f='a^x+exp(b)+sin(c*x)+d'
  1. 评估:

    y = eval(f)

    或(rahnema1 建议)

    fHandle = eval(['@(x, a, b, c, d) ' f]);
    y = fHandle(x, a, b, c, d);
  2. str2func:

    fHandle = str2func(['@(x, a, b, c, d) ' f]);
    y = fHandle(x, a, b, c, d);

除了performance reasons,为什么第一个选项比第二个选项差?

备注

  • 请注意,我知道最好尽可能避免这两种方法。

  • 请注意,我将 eval 的输出分配给一个变量,这样可以避免执行很多棘手的代码。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com