[sf-perl] Can't figure this one out

Joe Brenner doom at kzsu.stanford.edu
Wed Mar 5 17:51:56 PST 2008


Steve Fink <sphink at gmail.com> wrote:
> >  > By the way
> >  >
> >  > if ($action =~ /start/i) {
> >  >         &MainPage($q, \%p);
> >  > } elsif ($action =~ /new|del|edt|save/i ) {
> >  >
> >  > {
> >  >
> >  > seems better to me
> >
> >  It's more compact, but I bet it runs slower.  This might even be a
> >  case where performance matters (i.e. if the cgi script sees a lot of
> >  traffic).
>
> I'll take that bet. I bet the all-regex version is faster. Especially
> on newer versions of Perl that have the trie optimization, but even
> without that I'd guess that it's faster.

And you may already be a winner.

I did a quickie benchmark and it looks like the regexp alternation
version is around 17% faster even in the "worst" cases (running on
perl v5.8.7).  I've seen some regexps with heavy reliance on alternation
that ran pretty slow, but this isn't one of them (and that was
a while back in any case).

Also, you (and Andy Lester) are probably right about it being a silly
thing to worry about -- this session on premature optimization is now
over.


Code and results follow:

my $count = 10000000;

my $result =
timethese($count, {
                   'logical_ors - start'        => 'chooser_mark_1( "start" )',
                   'regexp_alternation - start' => 'chooser_mark_2( "start" )',
                  });

$result =
timethese($count, {
                   'logical_ors - edt'        => 'chooser_mark_1( "edt" )',
                   'regexp_alternation - edt' => 'chooser_mark_2( "edt" )',
                  });


$result =
timethese($count, {
                   'logical_ors - save'        => 'chooser_mark_1( "save" )',
                   'regexp_alternation - save' => 'chooser_mark_2( "save" )',
                  });

sub chooser_mark_1 {
  my $action = shift;
  my $branch;
  if ($action =~ /start/i) {
    $branch = 1;
  } elsif ($action =~ /new/i ||
           $action =~ /del/i ||
           $action =~ /edt/i ||
           $action =~ /save/i ) {
    $branch = 2;
  }
  return $branch;
}


sub chooser_mark_2 {
  my $action = shift;
  my $branch;
  if ($action =~ /start/i) {
    $branch = 1;
  } elsif ($action =~ /new|del|edt|save/i ) {
    $branch = 2;
  }
  return $branch;
}


Results:

Benchmark: timing 10000000 iterations of logical_ors - start, regexp_alternation - start...
logical_ors - start: 18 wallclock secs (16.96 usr +  0.33 sys = 17.29 CPU) @ 578369.00/s (n=10000000)
regexp_alternation - start: 18 wallclock secs (16.83 usr +  0.33 sys = 17.16 CPU) @ 582750.58/s (n=10000000)
Benchmark: timing 10000000 iterations of logical_ors - edt, regexp_alternation - edt...
logical_ors - edt: 22 wallclock secs (21.67 usr +  0.42 sys = 22.09 CPU) @ 452693.53/s (n=10000000)
regexp_alternation - edt: 17 wallclock secs (18.15 usr +  0.37 sys = 18.52 CPU) @ 539956.80/s (n=10000000)
Benchmark: timing 10000000 iterations of logical_ors - save, regexp_alternation - save...
logical_ors - save: 23 wallclock secs (23.53 usr +  0.48 sys = 24.01 CPU) @ 416493.13/s (n=10000000)
regexp_alternation - save: 19 wallclock secs (18.46 usr +  0.36 sys = 18.82 CPU) @ 531349.63/s (n=10000000)





> _______________________________________________
> SanFrancisco-pm mailing list
> SanFrancisco-pm at pm.org
> http://mail.pm.org/mailman/listinfo/sanfrancisco-pm


More information about the SanFrancisco-pm mailing list