Your skill will accomplish what the force of many cannot |
|
PerlMonks |
Re: How To Count Lines In File?by tomhukins (Curate) |
on Jan 27, 2003 at 00:13 UTC ( [id://230085]=note: print w/replies, xml ) | Need Help?? |
Here's an answer to your question, instead of yet another way to count the number of lines in a file. ;-) The solution mentioned in the FAQ runs much faster. Run the following code:
So, why does this happen? Well, the read_line approach above must read the file one byte at a time in case it encounters a line ending. The read_block approach reads a block of data from the disk and processes it within the Perl process, not needing to make any operating system calls. The significance of 4096 is that disk block sizes are usually some multiple of 1024 bytes, so reading complete blocks helps the code run faster than if it were to read partial blocks.
In Section
Seekers of Perl Wisdom
|
|