Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: How To Count Lines In File?

by tomhukins (Curate)
on Jan 27, 2003 at 00:13 UTC ( #230085=note: print w/replies, xml ) Need Help??


in reply to How To Count Lines In File?

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:

#!/usr/bin/perl use strict; use warnings; use Benchmark qw(timethese); my $filename = '/usr/share/dict/words'; timethese(100, { 'read_block' => sub { open(FILE, $filename) or die "Can't open file: $!"; my $lines = 0; while (read FILE, my $buffer, 4096) { $lines += ($buffer =~ tr/\n//); } close FILE; }, 'read_line' => sub { open(FILE, $filename) or die "Can't open file: $!"; while (<FILE>) {}; my $lines = $.; close FILE; } });

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://230085]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2021-01-27 10:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?