[DGBI] oops, obviously I've missed something (was: regexp or array?)



On 2009-02-18, Peter Makholm <peter@xxxxxxxxxxx> wrote:
End of Road <joblo@xxxxxxxxx> writes:

Using regular expressions

$strs = 'str1|str2|str3";
if ($str =~ /strs/)
...

Or using an array

@strs = qw/str1,str2, str3/;
foreach (@strs) {
if ($_[0] =~ /$_/)
...

Better use an array of precompiled regexpes:

my @regexp = map { qr/$_/ } qw(foo bar baz);

if( grep { $_[0] =~ $_ } @regexp ) {

...

}

perl -wle '
use Benchmark qw|cmpthese timethese|;
my @arg = qw{ aa cc ee ff };
my $x = qr{a|b|c};
my $y = q{a|b|c};
cmpthese timethese -10, {
pure => sub { m{a|b|c} foreach @arg },
prec => sub { m{$x} foreach @arg },
expl => sub { $_ =~ $x foreach @arg },
alt => sub { $x foreach @arg },
eval => sub { m{$y} foreach @arg },
};
'
Useless use of private variable in void context at -e line 10.
Benchmark:
running
alt, eval, expl, prec, pure
for at least 10 CPU seconds
....

alt: 12 wallclock secs (10.38 usr + 0.02 sys = 10.40 CPU) @ 267620.77/s (n=2783256)

eval: 11 wallclock secs (10.10 usr + 0.04 sys = 10.14 CPU) @ 57220.81/s (n=580219)

expl: 11 wallclock secs (10.52 usr + 0.04 sys = 10.56 CPU) @ 37743.47/s (n=398571)

prec: 11 wallclock secs (10.56 usr + 0.02 sys = 10.58 CPU) @ 38041.49/s (n=402479)

pure: 11 wallclock secs (10.36 usr + 0.02 sys = 10.38 CPU) @ 61972.35/s (n=643273)

Rate expl prec eval pure alt
expl 37743/s -- -1% -34% -39% -86%
prec 38041/s 1% -- -34% -39% -86%
eval 57221/s 52% 50% -- -8% -79%
pure 61972/s 64% 63% 8% -- -77%
alt 267621/s 609% 603% 368% 332% --

I've checked 4 times, C<expl> and C<prec> flip-flops, while
C<eval> and C<pure> are stable. What I've missed this time?

C<alt> seems to be a *way* fast, right? Left as an excersise for curious
readers. (spoiler, since rot13 is letter only, 100 empty lines below)





































































































perl -wle '
$x = qr{(?{ print 1 })};
@y = qr{ a b c };
$x foreach (@y)'
Useless use of a variable in void context at -e line 4.

*CUT*

--
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom
.



Relevant Pages

  • parse hash by array element seems to run slow
    ... I've been through the faq's and every web site that focuses on perl ... I iterate the array and search the hash keys to find a match and then ... foreach $verifyArr ...
    (comp.lang.perl.misc)
  • Re: How to get non-unique elements from an array?
    ... These are WAY off of benchmark tests, which shows Lyndon's script just killing all others.... ... What I'd like to see is if Perl is faster than Ruby at this job... ... inefficient array processing on my part. ...
    (comp.lang.ruby)
  • Re: math formula substitution and evaluation
    ... (Brackets return a reference to an anonymous array containing the list, ... converting the character to its ascii value and ... foreach my $test { ... If the tests really do look like what you've displayed, then the easiest way would be to make sure your tests are proper perl code, and then run them through "eval". ...
    (perl.beginners)
  • Re: read and process a file each 3 lines
    ... I am trying to write a second script, in perl, that would print the ... above-mentionned array, and then reckon each value encountered in my ... foreach my $i ...
    (comp.unix.shell)
  • Re: foreach statement output
    ... know about foreach loop workings. ... I've got an incoming array of unknown ... The Perl Cookbook ... My big confusion over this foreach issue is I failed to realize that the ...
    (comp.infosystems.www.authoring.cgi)