Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners > Re: opening a b...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 2 Topic 10968 of 11520
Post > Topic >>

Re: opening a big file

by noreply@[EMAIL PROTECTED] (Gunnar Hjalmarsson) Apr 21, 2008 at 05:18 PM

Mr. Shawn H. Corey wrote:
> The fastest way to do this is to read every line into Perl and disregard

> everything not relevant.

Don't think so.

I did a benchmark on a text file with 100,000 lines, where I'm actually 
only interested in the 5 last lines. Except for Tie::File, which proved 
to be awfully slow, reading and testing every line against a regex took 
hundreds of times longer time compared to seek() or File::ReadBackwards. 
Please see below.

C:\home>type test.pl
use File::ReadBackwards;
use Tie::File;
use Benchmark 'cmpthese';

open my $fh, '>', 'test.txt' or die $!;
for ( 1..100000 ) {
     print $fh
       join('', map { (0..9,'A'..'Z','a'..'z')[rand 62] } 1..80), "\n";
}
close $fh;

cmpthese -10, {
     bw        => sub {
         my $bw = File::ReadBackwards->new('test.txt') or die $!;
         for ( 1..5 ) { my $line = $bw->readline; $line =~ /abc/ }
     },
     seek      => sub {
         open my $fh, '<', 'test.txt' or die $!;
         seek $fh, -500, 2 or die $!;
         <$fh>;
         while ( <$fh> ) { /abc/ }
     },
     tf        => sub {
         tie my @[EMAIL PROTECTED]
 'Tie::File', 'test.txt' or die $!;
         for ( -5 .. -1 ) { $file[$_] =~ /abc/ }
     },
     all_lines => sub {
         open my $fh, '<', 'test.txt' or die $!;
         while ( <$fh> ) { /abc/ }
     },
};

C:\home>test.pl
              Rate        tf all_lines        bw      seek
tf        0.877/s        --      -87%     -100%     -100%
all_lines  6.54/s      646%        --     -100%     -100%
bw         1781/s   202999%    27116%        --      -74%
seek       6743/s   769060%   102971%      279%        --

C:\home>

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 




 2 Posts in Topic:
Re: opening a big file
shawnhcorey@[EMAIL PROTEC  2008-04-20 20:55:43 
Re: opening a big file
noreply@[EMAIL PROTECTED]  2008-04-21 17:18:07 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 13:15:36 CDT 2008.