gpt4 book ai didi

performance - Perl:为什么 if 语句比 "and"慢?

转载 作者:行者123 更新时间:2023-12-04 19:56:29 25 4
gpt4 key购买 nike

在 Perl 中,条件可以表示为

if (condition) { do something }

或作为

(condition) and do { do something }

有趣的是,第二种方式似乎快了大约 10%。有谁知道为什么吗?

最佳答案

下面是关于 deparse 的一些评论:

首先,不要使用 B::Terse,它已经过时了。一旦您习惯了 B::Concise,它就会为您提供更好的信息。

其次,您使用给定的文字代码运行它,因此条件被视为恰好为真的裸词,因此在这两种情况下都优化了 bool 检查,这有点违背了目的。

第三,没有额外的操作码 - “null”表示已优化掉的操作码(完全脱离执行树,但仍在解析树中。)

这是两种情况的简明执行树,显示它们是相同的:

$ perl -MO=Concise,-exec -e'($condition) and do { do something }'
1 <0> enter
2 <;> nextstate(main 2 -e:1) v
3 <#> gvsv[*condition] s
4 <|> and(other->5) vK/1
5 <$> const[PV "something"] s/BARE
6 <1> dofile vK/1
7 <@> leave[1 ref] vKP/REFC
-e syntax OK
$ perl -MO=Concise,-exec -e'if ($condition) { do something }'
1 <0> enter
2 <;> nextstate(main 3 -e:1) v
3 <#> gvsv[*condition] s
4 <|> and(other->5) vK/1
5 <$> const[PV "something"] s/BARE
6 <1> dofile vK/1
7 <@> leave[1 ref] vKP/REFC
-e syntax OK

关于performance - Perl:为什么 if 语句比 "and"慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/88546/

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