Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Benchmark - How to get ($1, $2, ...)?

by ferreira (Chaplain)
on Feb 17, 2007 at 18:55 UTC ( #600620=note: print w/replies, xml ) Need Help??


in reply to How to get ($1, $2, ...)?

Ok. To conclude my explorations on the issue of this node, something which can be eventually useful for others, I summarized the three solutions to "get ( $1, $2, ... )" favoured in this thread and made a little benchmark on them.

There is the solution which uses @- and @+: no messing with magic $<n> variables, but needs to know about the variable it was matched against.

# @groups = groups1($s) sub groups1 { return map { substr $_[0], $-[$_], $+[$_] - $-[$_] } 1..$#- }

There is the beautiful solution by educated_foo with symbolic references:

# educated_foo sub groups2 { no strict 'refs'; return map { $$_ } 1..$#- }

And the quick solution given by demerphq:

# demerphq sub groups3 { return eval '($'.join(',$',1..$#-).')' }

which happens to use eval.

The general result for 5.8.8 looks like

s/iter demerphq @- and @- educated_foo demerphq 6.65 -- -44% -50% @- and @- 3.72 79% -- -10% educated_foo 3.34 99% 11% --

which suggests eval imposes a high performance penalty, making it half as fast as the educated_foo's version, which is followed closely by the solution using @- and @+ (but this never surpasses the former). This was tested on four 5.8.8 architectures: i386-freebsd-64int, cygwin, MSWin32-x86-multi-thread (vanilla-perl), MSWin32-x86-multi-thread.

Results has shown greater variances at other versions and architectures, like v5.8.2 built for PA-RISC1.1-thread-multi, v5.8.7 built for i686-linux-thread-multi, v5.8.4 built for MSWin32-x86-multi-thread, but the order kept the same. The winner was faster than the 'eval' version with percentages ranging from 65% to 145%. But as the interpreter development has seen a lot of changes up to the code of 5.8.8, I preferred to concentrate on 5.8.8 perls. Maybe 5.8.8 tested on different processor architectures like PA-RISC and PPC may reveal more trade-offs than this partial benchmark.

The benchmark code used was:

#!/usr/bin/perl # @groups = groups1($s) sub groups1 { return map { substr $_[0], $-[$_], $+[$_] - $-[$_] } 1..$#- } # educated_foo sub groups2 { no strict 'refs'; return map { $$_ } 1..$#- } # demerphq sub groups3 { return eval '($'.join(',$',1..$#-).')' # return '($'.join(',$',1..$#-).')' } use constant MAX_GROUPS => 5; # use < 26 which corresponds to 'z' use constant MAX_REPEAT => 20; use constant ITERATIONS => 20000; use constant RANDOM_SEED => 666; # $re = random_re($n) # a regex with $n groups qr/(a*)(b*)...(z*)/ # use $n >= 1 to avoid the subtleties of the empty regex qr// sub random_re { my $n = shift; my $re = join '', map { "($_*)" } 'a' .. chr(ord('a')+$n-1); return qr/$re/ } # $text = random_text($n) # a text which matches the random regex produced by random_re sub random_text { my $n = shift; return join '', map { $_ x int(rand(MAX_REPEAT)) } 'a' .. chr(ord( +'a')+$n-1) } sub run_code { my $sub = shift; srand(RANDOM_SEED); # always the same seed - so that it is reprodu +cible for (1..ITERATIONS) { $n = int(rand(MAX_GROUPS))+1; $re = random_re($n); $text = random_text($n); # print "n: $n, re: $re, text: $text\n"; die "not ok: $text =~ /$re/" unless $text =~ /$re/; # assertio +n @groups = $sub->($text); # print "groups 1: @groups\n"; } } #run_code(\&groups2); #exit; use Benchmark qw(cmpthese :hireswallclock ); use constant MIN_RUNTIME => 30; # benchmark minimum time: ? CPU second +s cmpthese(-MIN_RUNTIME(), { '@- and @-' => sub { run_code(\&groups1) }, 'educated_foo' => sub { run_code(\&groups2) }, 'demerphq' => sub { run_code(\&groups3) }, });

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2019-06-16 11:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Is there a future for codeless software?



    Results (76 votes). Check out past polls.

    Notices?