gpt4 book ai didi

perl - perl 是否在一定范围内优化 foreach?

转载 作者:行者123 更新时间:2023-12-04 22:09:58 24 4
gpt4 key购买 nike

给定一个简单的程序:

use strict;
use warnings;

my $count = 0;

foreach ('a' .. 'zzzzzzzzzz') {
$count++;
}

print "$count\n";
perl 是否会在内存中创建大约 1.4+e14 元素的整个范围,然后迭代它们,或者它是否有任何内部优化来一次简单地跨过一个范围运算符的状态?

嗯,我实际上已经运行了,所以来自 top 的经验数据表明它是后者(并且它还没有完成看到来自 time 的数据),但是它是否记录在任何地方或在哪里可以在来源中找到确认?

最佳答案

来自 perldoc perlop :

In the current implementation, no temporary array is created when the range operator is used as the expression in foreach loops, but older versions of Perl might burn a lot of memory when you write something like [ what you wrote ]

关于perl - perl 是否在一定范围内优化 foreach?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45483137/

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