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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|