每次到达该行时,解释语言的代码都会重新解释吗?

发布于 2024-07-21 20:20:47 字数 331 浏览 4 评论 0原文

假设没有为程序生成字节码,例如在 Ruby、Perl 或 PHP 中,在这种情况下,每次执行再次到达第 1 行时,下面的第 1 行是否都会重新解释?

while ($indexArrayMoviesData < $countArrayMoviesData + $countNewlyAddedMoviesData) {
  # do something
}

也就是说,如果循环运行 100,000 次,那么该行将被重新解释 100,000 次?

如果是这样,字节码创建不仅有助于程序的初始启动,而且有助于执行过程? (因为代码不需要再次重新解释)

suppose that no bytecode is generated for a program, like in Ruby, Perl, or PHP, in this case, is line 1 below re-interpreted every time the execution reach line 1 again?

while ($indexArrayMoviesData < $countArrayMoviesData + $countNewlyAddedMoviesData) {
  # do something
}

that is, if the loop runs 100,000 times, then that line will be re-interpreted 100,000 times?

and if so, the bytecode creation helps not only the initial start up of he program, but also during the execution? (because code doesn't need to be re-interpreted again)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

去了角落 2024-07-28 20:20:47

通常,它将被转换为字节代码,然后执行该字节代码。

但以 PHP 为例,字节码会在每个请求/页面视图上重新生成。 除非您安装了字节码(或在 PHP 中通常称为操作码)缓存,例如 XCache、APC 或 EAccelerator。

Typically, it'll be converted into byte code and that byte code will then be executed.

But in the case of PHP for example, the byte code is regenerated on every request/page view. Unless you install a byte code (or opcode as it's called often in the case of PHP) cache, such as XCache, APC or EAccelerator.

温柔戏命师 2024-07-28 20:20:47

对于最近的语言,包括 perl,代码在执行之前会被预编译。 所以大部分分析工作只执行一次。

shell 的情况并非如此,shell 每次执行时都会解释每一行。

For recent languages, including perl, the code is precompiled before being executed. So most of the analysis work is performed only once.

This is not the case for shells, which interpret every line each time they execute them.

堇年纸鸢 2024-07-28 20:20:47

如果解释器是明智的,它希望检查 $countArrayMoviesData 或 $countNewlyAddedMoviesData 在循环期间是否被更改,如果没有,则可以计算并保留总和。

如果这些值在循环内更新,那么字节码很可能也需要进行加法操作,而不会提高效率。

If the interpreter is sensible it would hopefully check if $countArrayMoviesData or $countNewlyAddedMoviesData were altered during the loop and if they weren't then the sum could be calculated and kept.

If the values are updated within the loop then in all likelihood even the bytecode would require an addition operation to take place, not making it any more efficient.

久隐师 2024-07-28 20:20:47

很少有口译员会这样做。 一个例子是古老的、不再使用的 Hypercard 的 Hypertalk 解释器,您实际上可以通过编程方式重写代码文本(它只是一个字符串!)

即使不生成字节代码的解释器也会首先解析您的代码,因为它是逐行完成很难,一次性完成则容易得多。 因此,一个非常简单的解释器基本上会有一棵树,其中包含一个用于“where”循环的节点和两个子节点:一个用于条件的“小于”表达式,一个用于循环体的块。

Very, very few interpreters will do this. An example is the ages-old, no longer used Hypertalk interpreter for Hypercard where you could actually rewrite the text of your code programatically (it's just a string!)

Even interpreters that don't produce byte code will parse your code first, as it's hard to do that line by line and much easier to do it all at once. So a really simple interpreter will basically have a tree, with a node for the "where" loop with two children: one "less than" expression for the conditional, and one block for the body of the loop.

愁以何悠 2024-07-28 20:20:47

正如所有顾问都知道的那样,您问题的答案是“视情况而定”。

你是对的,在某些解释性语言中,该行可能每次都会被重新解释。 我怀疑大多数 shell 都是这样处理的。

Basic 的原始版本也是这样做的。

大多数当前的解释器都会至少对语言进行标记,这样就不需要每次都重新扫描文本。 也就是说,类似 BASIC 的程序

 00010 LET A=42
 00020 DO WHILE A > 0
 00025    LET A = A - 1
 00030 ENDDO

至少会将其转换为关键字的小标记和变量的地址,就像

 LET    $0003, 42
 LABEL  00020 
 LETEST A, 0
 IFTRUEGOTO   00030
 SUB    $0005, $0003, 1
 GOTO   00020
 LABEL  00030

翻译中每个大写单词在内部都是一个整数一样。 这样,就有一个词法分析过程来翻译它,然后解释器就能够解释标记值。

当然,一旦你走到这一步,你会发现自己在想“哎呀,为什么不使用真正的操作码呢?”

The answer to your question, as all consultants know, is "it depends."

You're right, in some interpreted languages, that line may be reinterpreted each time. I suspect most shells handle it roughly this way.

The original versions of Basic also did it this way.

Most current interpreters will at least tokenize the language, so that the text doesn't need to be re-scanned each time. That is, a BASIC-ish program like

 00010 LET A=42
 00020 DO WHILE A > 0
 00025    LET A = A - 1
 00030 ENDDO

would convert it at the least to small tokens for the keywords, and addresses for the variable, something like

 LET    $0003, 42
 LABEL  00020 
 LETEST A, 0
 IFTRUEGOTO   00030
 SUB    $0005, $0003, 1
 GOTO   00020
 LABEL  00030

where each word in upper case in the translation is a single integer internally. That way, there's a single lexical analysis pass to translate it, followed by the interpreter being able to just interpret the token values.

Of course, once you go that far, you find yourself thinking "gee, why not use real opcodes?"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文