Then I made some tests using the Benchmark module#!/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 );
And then when I ran my test I got some interesting results (this took a while btw ;)#!/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()' } );
Can somebody help me analyze the results?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)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Groking grep
by Trinary (Pilgrim) on Feb 06, 2001 at 23:59 UTC | |
by spaz (Pilgrim) on Feb 07, 2001 at 00:04 UTC | |
|
Re: Groking grep
by Anonymous Monk on Feb 07, 2001 at 02:07 UTC | |
|
Re: Groking grep
by dws (Chancellor) on Feb 06, 2001 at 23:56 UTC | |
by spaz (Pilgrim) on Feb 07, 2001 at 00:01 UTC |