在Perl或Perl之间迭代文件有什么区别? [英] What's the difference between iterating over a file with foreach or while in Perl?
问题描述
我在Perl中有一个文件句柄 FILE
,我想迭代文件中的所有行。以下之间有区别吗?
I have a filehandle FILE
in Perl, and I want to iterate over all the lines in the file. Is there a difference between the following?
while (<FILE>) {
# do something
}
和
foreach (<FILE>) {
# do something
}
推荐答案
对于大多数用途,您可能不会注意到差异。但是, foreach
将每行读入列表(不是数组),然后逐行查看,而而
一次读取一行。因为 foreach
会使用更多的内存并且需要处理时间,所以通常建议使用而
来遍历一个文件。
For most purposes, you probably won't notice a difference. However, foreach
reads each line into a list (not an array) before going through it line by line, whereas while
reads one line at a time. As foreach
will use more memory and require processing time upfront, it is generally recommended to use while
to iterate through lines of a file.
编辑(通过Schwern): foreach
循环相当于:
EDIT (via Schwern): The foreach
loop is equivalent to this:
my @lines = <$fh>;
for my $line (@lines) {
...
}
不幸的是,Perl并没有优化这个特殊情况,因为它与范围运算符( 1..10
)
It's unfortunate that Perl doesn't optimize this special case as it does with the range operator (1..10
).
例如,如果我在之间读取/ usr / share / dict / c $ c>循环并让它们睡眠完成后,我可以使用 ps
来查看进程占用多少内存。作为一个控件,我已经包括一个打开该文件的程序,但不做任何操作。
For example, if I read /usr/share/dict/words with a for
loop and a while
loop and have them sleep when they're done I can use ps
to see how much memory the process is consuming. As a control I've included a program that opens the file but does nothing with it.
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
schwern 73019 0.0 1.6 625552 33688 s000 S 2:47PM 0:00.24 perl -wle open my $fh, shift; for(<$fh>) { 1 } print "Done"; sleep 999 /usr/share/dict/words
schwern 73018 0.0 0.1 601096 1236 s000 S 2:46PM 0:00.09 perl -wle open my $fh, shift; while(<$fh>) { 1 } print "Done"; sleep 999 /usr/share/dict/words
schwern 73081 0.0 0.1 601096 1168 s000 S 2:55PM 0:00.00 perl -wle open my $fh, shift; print "Done"; sleep 999 /usr/share/dict/words
程序正在消耗几乎32兆的真实内存( RSS
列)来存储我的2.4兆/ usr / share / dict / words的内容。 while
循环仅在一段时间内只存储一行,线路缓冲为70k。
The for
program is consuming almost 32 megs of real memory (the RSS
column) to store the contents of my 2.4 meg /usr/share/dict/words. The while
loop only stores one line at a time consuming just 70k for line buffering.
这篇关于在Perl或Perl之间迭代文件有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!