Monday, August 25, 2008

Difference between for (<STDIN>) and while(<STDIN>) in Perl

Can you tell if there is a difference between "for (<STDIN>) { BLOCK}" and "while(<STDIN>) {BLOCK}" in Perl? They both might look like similar. When I used the first construct and gave a huge file as input, my Perl program crashed. When checked the top output after starting the program, I figured that the RSS size of the program kept growing and eventually crashed. Hmm ... when I switched to the second construct, not only the program ran successfully but also used less memory!

The difference is that for construct reads the entire file before it enters into the loop. But the while construct reads only a part of the file and buffers it. I wrote a program to count the number of lines in an input file in both the flavors. while loop version took only 1MB where as for loop version took 149 MB for an input file of 79 MB. On top of it, the while loop version took on an average 1.36 seconds, and the for loop version took around 2 seconds.

So if you are writing any program in Perl that reads files line by line, you must stick to the while() construct if you would like your program not to crash on large file inputs and to run faster.

1 comment:

nena_uci said...

Thank u!!! That is a good practice :)