Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Threads From Hell #2: How To Parse A Very Huge File

by marioroy (Prior)
on May 24, 2015 at 03:48 UTC ( [id://1127550]=note: print w/replies, xml ) Need Help??


in reply to Threads From Hell #2: How To Search A Very Huge File [SOLVED]

The assumptions and testing are valid. However, MCE is quite fast at this. MCE has bin/mce_grep (a parallel wrapper for the grep binary) and examples/egrep.pl (100% Perl code). Both run faster than the grep command.

$ time grep karl very_huge.file nose cuke karl real 0m2.127s user 0m1.845s sys 0m0.283s $ time ./MCE-1.608/bin/mce_grep karl very_huge.file nose cuke karl real 0m1.061s user 0m2.176s sys 0m1.616s $ time ./MCE-1.608/examples/egrep.pl karl very_huge.file nose cuke karl real 0m0.690s user 0m2.165s sys 0m0.362s

The MCE::Grep has an alternative mode by appending the "_f" suffix and reading the file directly. That runs in 8.5 seconds. The overhead is from calling the code block once per each line. Thus, use egrep.pl residing inside the examples directory.

# open( my $fh, '<', 'very_huge.file' ); # my @result = mce_grep { /karl/ } $fh; # close $fh; my @result = mce_grep_f { /karl/ } 'very_huge.file';

The following code snippet parses the 2 GiB file in 1 second.

use MCE::Loop; use Time::HiRes qw( time ); MCE::Loop::init( { max_workers => 4, use_slurpio => 1 } ); my $start = time; my $pattern = 'karl'; my @result = mce_loop_f { my ($mce, $slurp_ref, $chunk_id) = @_; ## Quickly determine if a match is found. ## Basically, only process slurped chunk if true. if ($$slurp_ref =~ /$pattern/im) { my @matches; open my $MEM_FH, '<', $slurp_ref; binmode $MEM_FH, ':raw'; while (<$MEM_FH>) { push @matches, $_ if (/$pattern/); } close $MEM_FH; MCE->gather(@matches); } } 'very_huge.file'; print join('', @result); printf "Took %.3f seconds\n", time - $start;

Replies are listed 'Best First'.
Re^2: Threads From Hell #2: How To Parse A Very Huge File
by BrowserUk (Patriarch) on May 24, 2015 at 07:56 UTC

    And then there's:

    $ time grep karl very_huge.file nose cuke karl real: 2.127s == user: 1.845s + sys: 0.283s ## CLOSE ENOUGH! $ time ./MCE-1.608/bin/mce_grep karl very_huge.file nose cuke karl real: 1.061s != user: 2.176s + sys: 1.616s ## NOT EVEN CLOSE to: 3. +792s $ time ./MCE-1.608/examples/egrep.pl karl very_huge.file nose cuke karl real: 0.690s != user: 2.165s + sys: 0.362s ## NOR IS THIS CLOSE to: + 2.527s

    Looks dodgy to me.

    And then the claim that:

    The following code snippet parses the 2 GiB file in 1 second.

    Let's examine that. The code: slurps the entire 2GiB file into memory and then starts 4 workers that each get a reference to (1/4 of???) the slurped data and then:

    1. Run a regex against their chunk of the file to see if it contains the sought string;
    2. And if it does, open their referenced chunk of the file as a memory file, and then re-run the regex on a line-by-line basis in order to get the whole lines that contain the sought string;
    3. And then they return those lines to the parent process (via a pipe??) for it to print out.

    So the regex is run against 1.25 times as much data as is contained in the file, and takes "less than one second"; which is less than the 2.127 seconds the real grep takes, despite that it only processes the file's data once?

    Have you heard the song: Funny numbers?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

      Chunking is applied following a bank-teller model. Thus, MCE does not read the entire file into memory.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-25 10:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found