After visiting this node I realized that I wasn't too sure when to use grep(1), perl, or perl's grep
Here are the results of my experiment: First I created a data file
#!/usr/bin/perl -w use strict; my $a; my $i; open( OUT, ">data.txt" ); for( $i = 0; $i < 1000000; $i++ ) { $a = rand( 126 ); $a = int( $a ); until( $a > 33 ) { $a = rand( 126 ); $a = int( $a ); } print OUT chr( $a ); print OUT "\n" if( $i % 80 == 0); } close( OUT );
Then I made some tests using the Benchmark module
#!/usr/bin/perl use Benchmark; sub child_grepping { $a = `/bin/grep 'S;Dvg&T?sBu=\@j4qkP&O' data.txt`; } sub inline_grepping { open( IN, "data.txt" ); @lines = <IN>; close( IN ); ($a) = grep /S;Dvg&T\?sBu=\@j4qkP&O/, @lines; } sub child_perlizing { my $command = "perl -e 'while( \$line = <> ) { if( \$line =~ "; $command .= "/S;Dvg&T\\\?sBu=\\\@j4qkP&O/) { print \$line; last; } + }' "; $command .= '< data.txt'; $a = `$command`; } sub inline_perlizing { my $line; open( IN, "data.txt" ); while( $line = <IN> ) { if( $line =~ /S;Dvg&T\?sBu=\@j4qkP&O/ ) { $a = $line; last; } } close( IN ); } timethese( 1000, { child_grepping => 'child_grepping()', child_perlizing => 'child_perlizing()', inline_grepping => 'inline_grepping()', inline_perlizing => 'inline_perlizing()' } );
And then when I ran my test I got some interesting results (this took a while btw ;)
Benchmark: timing 1000 iterations of child_grepping, child_perlizing, inline_grepping, inline_perlizing... child_grepping: 15 wallclock secs ( 0.17 usr 0.62 sys + 7.27 cusr 7.17 csys = 0. +00 CPU) child_perlizing: 90 wallclock secs ( 0.17 usr 0.60 sys + 75.91 cusr 6.87 csys = 0. +00 CPU) inline_grepping: 191 wallclock secs (177.97 usr + 7.64 sys = 185.61 CPU) inline_perlizing: 66 wallclock secs (59.68 usr + 1.19 sys = 60.87 CPU)
Can somebody help me analyze the results?

-- Dave

In reply to Groking grep by spaz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.