I am a newbie and have been following this thread since I am interested in
benchmarking.

So I copied the code and ran it on my machine. I have a 3.5 MHz system
runing Windows XP. I am using ActivePerl 3.8.6.

On my machine, benchmark complained about too few iterations. So I modified
the script in this manner:
1. Changed the iterations to 10,000,000.
2. Used :hireswallclock.
3. Added the pos-regex code.
4. Also called on cmpthese since the display is easier to read and gives a
more intuitive feel for the results.

The bench2 code and results are shown below.

The results essential agree with the results of the original script.

I did some experimentation.  In the man page, the examples have the tested
code inside single quotes while the suggested benchmarking script uses
anonymous subroutines. So I edited the script to eliminate the subroutines
and placed the code to be tested inside single quotes.

The bench3 code and results are also shown below.

The results are totally different. Every code segment run faster and the
regexs are way ahead of substrings.

I ran other experiments and I have come to the following conclusions:

1. number of iterations (time that test code runs) is an important factor. Too short and the results vary
greatly between runs. I found that too many iteations tended to provide inconsistent results between runs.


2. running too many segment codes in one benchmark has the tendency to produce inconsistent results.
it is best to only tests two to three code segments within one benchmark.


3.try to run the code segments within the same environments they will be run.
The anonymous subroutines proved to be very costly and produce totally different results.
I leave this to experts to provide an explanation.


4. how busy your machine is will greatly affect the benchmark results.

It is not my intent to cause a flame war. I was just trying to learn and verified the results.

My final 2 cents: It appears that the simplest regex perfomed best.


__BENCH__2__CODE__ #!perl

use strict ;
use warnings ;
use Benchmark qw(:hireswallclock cmpthese timethese);

my $string = '| B B B B |13145551212 B B B B |N| B B B B B B | B 0|001001|001001| 100|10|B|A|' ;

print "\n\nMultiple targets:\n\n" ;

my $results = timethese(10_000_000, {
'substr_regex_1' => sub { if ( substr( $string, 39, 1 ) =~ /[BNPG]/ ) { my $a = $_ } } ,
'substr_regex_3' => sub { if ( substr( $string, 38, 3 ) =~ /\|[BNPG]\|/ ) { my $a = $_ } } ,
'plain_substr' => sub { my $sub = substr( $string, 39, 1 ); if (
$sub eq "B" || $sub eq "N" || $sub eq "P" || $sub eq "G") { my $a = $sub } } ,
'plain_regex' => sub { if ( $string =~ /^.{38}\|[BNPG]\|/ ) { my $a = $_ } },
'regex_line' => sub { if ( $string =~ /\|[BNPG]\|/ ) { my $a = $_ } },
'pos_regex_3' => sub { pos ($string) = 38;
if ($string =~ /\|[BNPG]\|/ ) { my $a = $_ } },
'pos_regex_1' => sub { pos ($string) = 39;
if ($string =~ /[BNPG]/ ) { my $a = $_ } } }
) ;
print "\n";
cmpthese $results;
print "\n\nSingle target:\n\n" ;


$results = timethese(10_000_000, {
'substr_regex_1' => sub { if ( substr( $string, 39, 1 ) =~ /N/ ) { my $a = $_ } } ,
'substr_regex_3' => sub { if ( substr( $string, 38, 3 ) =~ /\|N\|/ ) { my $a = $_ } } ,
'plain_substr' => sub { if ( substr( $string, 39, 1 ) eq "N" ) { my $a = $_ } } ,
'plain_regex' => sub { if ( $string =~ /^.{38}\|N\|/ ) { my $a = $_ } },
'regex_line' => sub { if ( $string =~ /\|N\|/ ) { my $a = $_ } },
'pos_regex_3' => sub { pos ($string) = 38;
if ($string =~ /\|N\|/ ) { my $a = $_ } },
'pos_regex_1' => sub { pos ($string) = 39;
if ($string =~ /N/ ) { my $a = $_ } } }
) ;
print "\n";
cmpthese $results;


__BENCH__2__RESULTS__

Multiple targets:

Benchmark: timing 10000000 iterations of plain_regex, plain_substr, pos_regex_1, pos_regex_3, regex_line, substr_regex_1, substr_regex_3...
plain_regex: 6.26715 wallclock secs ( 6.14 usr + 0.01 sys = 6.16 CPU) @ 1624431.45/s (n=10000000)
plain_substr: 6.38174 wallclock secs ( 6.31 usr + 0.03 sys = 6.34 CPU) @ 1576044.13/s (n=10000000)
pos_regex_1: 6.57066 wallclock secs ( 6.56 usr + 0.00 sys = 6.56 CPU) @ 1523693.43/s (n=10000000)
pos_regex_3: 9.13073 wallclock secs ( 8.97 usr + 0.00 sys = 8.97 CPU) @ 1115075.83/s (n=10000000)
regex_line: 6.90791 wallclock secs ( 6.80 usr + 0.00 sys = 6.80 CPU) @ 1471237.31/s (n=10000000)
substr_regex_1: 5.06138 wallclock secs ( 5.09 usr + 0.00 sys = 5.09 CPU) @ 1963093.84/s (n=10000000)
substr_regex_3: 5.91842 wallclock secs ( 5.91 usr + -0.03 sys = 5.87 CPU) @ 1702127.66/s (n=10000000)


Rate pos_regex_3 regex_line pos_regex_1 plain_substr plain_regex substr_regex_3 substr_regex_1
pos_regex_3 076/s -- -24% -27% -29% -31% -34% -43%
regex_line 1471237/s 32% -- -3% -7% -9% -14% -25%
pos_regex_1 1523693/s 37% 4% -- -3% -6% -10% -22%
plain_substr 1576044/s 41% 7% 3% -- -3% -7% -20%
plain_regex 1624431/s 46% 10% 7% 3% -- -5% -17%
substr_regex_3 1702128/s 53% 16% 12% 8% 5% -- -13%
substr_regex_1 1963094/s 76% 33% 29% 25% 21% 15% --



Single target:

Benchmark: timing 10000000 iterations of plain_regex, plain_substr, pos_regex_1, pos_regex_3, regex_line, substr_regex_1, substr_regex_3...
plain_regex: 6.44568 wallclock secs ( 6.39 usr + 0.03 sys = 6.42 CPU) @ 1557147.31/s (n=10000000)
plain_substr: 3.59761 wallclock secs ( 3.56 usr + -0.00 sys = 3.56 CPU) @ 2807411.57/s (n=10000000)
pos_regex_1: 8.50115 wallclock secs ( 8.45 usr + -0.02 sys = 8.44 CPU) @ 1185255.42/s (n=10000000)
pos_regex_3: 6.4463 wallclock secs ( 6.42 usr + 0.00 sys = 6.42 CPU) @ 1557147.31/s (n=10000000)
regex_line: 3.90568 wallclock secs ( 3.92 usr + 0.00 sys = 3.92 CPU) @ 2550369.80/s (n=10000000)
substr_regex_1: 4.50092 wallclock secs ( 4.49 usr + 0.00 sys = 4.49 CPU) @ 2229654.40/s (n=10000000)
substr_regex_3: 4.49159 wallclock secs ( 4.48 usr + 0.00 sys = 4.48 CPU) @ 2230151.65/s (n=10000000)


Rate pos_regex_1 pos_regex_3 plain_regex substr_regex_1 substr_regex_3 regex_line plain_substr
pos_regex_1 /s -- -24% -24% -47% -47% -54% -58%
pos_regex_3 1557147/s % -- -0% -30% -30% -39% -45%
plain_regex 1557147/s 31% % -- -30% -30% -39% -45%
substr_regex_1 2229654/s 88% 43% -- -0% -13% -21%
substr_regex_3 2230152/s 88% 43% 43% 0% -- -13% -21%
regex_line 2550370/s 115% 64% 64% 14% 14% -- -9%
plain_substr 2807412/s 137% 80% 80% 26% 26% 10% --


__BENCH__3__CODE__
#!perl

use strict ;
use warnings ;
use Benchmark qw( :hireswallclock cmpthese timethese);

my $string = '| B B B B |13145551212 B B B B |N| B B B B B B | B 0|001001|001001| 100|10|B|A|' ;

print "\n\nMultiple targets:\n\n" ;

my $results = timethese(10_000_000, {
'substr_regex_1' => 'if ( substr( $string, 39, 1 ) =~ /[BNPG]/ ) { my $a = $_ }' ,
'substr_regex_3' => 'if ( substr( $string, 38, 3 ) =~ /\|[BNPG]\|/ ) { my $a = $_ }' ,
'plain_substr' => 'my $sub = substr( $string, 39, 1 );
if ($sub eq "B" || $sub eq "N" || $sub eq "P" || $sub eq "G") { my $a = $sub }' ,
'plain_regex' => 'if ( $string =~ /^.{38}\|[BNPG]\|/ ) { my $a = $_ }',
'regex_line' => 'if ( $string =~ /\|[BNPG]\|/ ) { my $a = $_ }',
'pos_regex_3' => 'pos ($string) = 38;
if ($string =~ /\|[BNPG]\|/ ) { my $a = $_ }',
'pos_regex_1' => 'pos ($string) = 39;
if ($string =~ /[BNPG]/ ) { my $a = $_ }' }
) ;
print "\n";
cmpthese $results;
print "\n\nSingle target:\n\n" ;


$results = timethese(10_000_000, {
'substr_regex_1' => 'if ( substr( $string, 39, 1 ) =~ /N/ ) { my $a = $_ }' ,
'substr_regex_3' => 'if ( substr( $string, 38, 3 ) =~ /\|N\|/ ) { my $a = $_ }' ,
'plain_substr' => 'if ( substr( $string, 39, 1 ) eq "N" ) { my $a = $_ }' ,
'plain_regex' => 'if ( $string =~ /^.{38}\|N\|/ ) { my $a = $_ }',
'regex_line' => 'if ( $string =~ /\|N\|/ ) { my $a = $_ }',
'pos_regex_3' => 'pos ($string) = 38;
if ($string =~ /\|N\|/ ) { my $a = $_ }',
'pos_regex_1' => 'pos ($string) = 39;
if ($string =~ /N/ ) { my $a = $_ }' }
) ;
print "\n";
cmpthese $results;


__BENCH__3__RESULTS__

Multiple targets:

Benchmark: timing 10000000 iterations of plain_regex, plain_substr, pos_regex_1, pos_regex_3, regex_line, substr_regex_1, substr_regex_3...
plain_regex: 1.4103 wallclock secs ( 1.41 usr + 0.00 sys = 1.41 CPU) @ 7107320.54/s (n=10000000)
plain_substr: 5.80651 wallclock secs ( 5.76 usr + 0.00 sys = 5.76 CPU) @ 1734605.38/s (n=10000000)
pos_regex_1: 3.61992 wallclock secs ( 3.61 usr + 0.00 sys = 3.61 CPU) @ 2770083.10/s (n=10000000)
pos_regex_3: 3.38102 wallclock secs ( 3.33 usr + 0.00 sys = 3.33 CPU) @ 3004807.69/s (n=10000000)
regex_line: 1.1214 wallclock secs ( 1.08 usr + 0.00 sys = 1.08 CPU) @ 9267840.59/s (n=10000000)
substr_regex_1: 1.81725 wallclock secs ( 1.83 usr + 0.00 sys = 1.83 CPU) @ 5470459.52/s (n=10000000)
substr_regex_3: 1.98296 wallclock secs ( 1.95 usr + 0.00 sys = 1.95 CPU) @ 5120327.70/s (n=10000000)


Rate plain_substr pos_regex_1 pos_regex_3 substr_regex_3 substr_regex_1 plain_regex regex_line
plain_substr s -- -37% -42% -66% -68% -76% -81%
pos_regex_1 2770083/s 0% -- -8% -46% -49% -61% -70%
pos_regex_3 3004808/s 73% % -- -41% -45% -58% -68%
substr_regex_3 5120328/s 195% 85% -- -6% -28% -45%
substr_regex_1 5470460/s 215% 97% 82% 7% -- -23% -41%
plain_regex 7107321/s 310% 157% 137% 39% 30% -- -23%
regex_line 9267841/s 434% 235% 208% 81% 69% 30% --



Single target:

Benchmark: timing 10000000 iterations of plain_regex, plain_substr, pos_regex_1, pos_regex_3, regex_line, substr_regex_1, substr_regex_3...
plain_regex: 1.06775 wallclock secs ( 1.08 usr + 0.00 sys = 1.08 CPU) @ 9276437.85/s (n=10000000)
plain_substr: 2.04142 wallclock secs ( 2.00 usr + 0.01 sys = 2.02 CPU) @ 4960317.46/s (n=10000000)
pos_regex_1: 3.51549 wallclock secs ( 3.52 usr + 0.02 sys = 3.53 CPU) @ 2831257.08/s (n=10000000)
pos_regex_3: 3.4822 wallclock secs ( 3.48 usr + 0.00 sys = 3.48 CPU) @ 2870264.06/s (n=10000000)
regex_line: 0.906903 wallclock secs ( 0.92 usr + 0.00 sys = 0.92 CPU) @ 10845986.98/s (n=10000000)
substr_regex_1: 1.99192 wallclock secs ( 1.98 usr + 0.02 sys = 2.00 CPU) @ 5000000.00/s (n=10000000)
substr_regex_3: 2.14122 wallclock secs ( 2.14 usr + 0.00 sys = 2.14 CPU) @ 4672897.20/s (n=10000000)


Rate pos_regex_1 pos_regex_3 substr_regex_3 plain_substr substr_regex_1 plain_regex regex_line
pos_regex_1 257/s -- -1% -39% -43% -43% -69% -74%
pos_regex_3 2870264/s 1% -- -39% -42% -43% -69% -74%
substr_regex_3 4672897/s 65% 3% -- -6% -7% -50% -57%
plain_substr 4960317/s 75% 73% 6% -- -1% -47% -54%
substr_regex_1 5000000/s 77% 74% 7% 1% -- -46% -54%
plain_regex 9276438/s 228% 223% 99% 87% 86% -- -14%
regex_line 10845987/s 283% 278% 132% 119% 117% 17% --


----- Original Message ----- Sent: Tuesday, January 25, 2005 11:45 AM

For the sake of comparison, here is a set of benckmarks for a couple
of different variations on the theme, including regexing the entire
line for the pattern, regexing the pattern at a known location, and
some substring and substring/regex combos.  for the heck of it, I also
tried just looking for one character, rather than a class:

CODE:

#!/usr/bin/perl

use strict ;
use warnings ;
use Benchmark ;


my $string = '| B B B B |13145551212 B B B B |N| B B B B B B | B 0|001001|001001| 100|10|B|A|' ;

print "\n\nMultiple targets:\n\n" ;

timethese(1000000,{
   'substr_regex_1' => sub { if ( substr( $string, 39, 1 ) =~
/[BNPG]/ ) { my $a = $_ } } ,
   'substr_regex_3' => sub { if ( substr( $string, 38, 3 ) =~
/\|[BNPG]\|/ ) { my $a = $_ } } ,
   'plain_substr'   => sub { my $sub = substr( $string, 39, 1 ); if
($sub eq "B" || $sub eq "N" || $sub eq "P" || $sub eq "G") { my $a =
$sub } } ,
   'plain_regex'    => sub { if ( $string =~ /^.{38}\|[BNPG]\|/ ) {
my $a = $_ } },
   'regex_line'     => sub { if ( $string =~ /\|[BNPG]\|/ ) { my $a =
$_ } }
   }) ;

print "\n\nSingle target:\n\n" ;

timethese(1000000,{
   'substr_regex_1' => sub { if ( substr( $string, 39, 1 ) =~ /N/ ) {
my $a = $_ } } ,
   'substr_regex_3' => sub { if ( substr( $string, 38, 3 ) =~ /\|N\|/
) { my $a = $_ } } ,
   'plain_substr'   => sub { if ( substr( $string, 39, 1 ) eq "N" ) {
my $a = $_ } } ,
   'plain_regex'    => sub { if ( $string =~ /^.{38}\|N\|/ ) { my $a =
$_ } },
   'regex_line'     => sub { if ( $string =~ /\|N\|/ ) { my $a = $_ } }
   }) ;

RESULTS (representative):

Multiple targets:

Benchmark: timing 1000000 iterations of plain_regex, plain_substr,
regex_line, substr_regex_1, substr_regex_3...
plain_regex:  2 wallclock secs ( 2.69 usr +  0.03 sys =  2.72 CPU) @
367647.06/s (n=1000000)
plain_substr:  3 wallclock secs ( 3.16 usr +  0.02 sys =  3.18 CPU) @
314465.41/s (n=1000000)
regex_line:  3 wallclock secs ( 3.32 usr +  0.00 sys =  3.32 CPU) @
301204.82/s (n=1000000)
substr_regex_1:  3 wallclock secs ( 2.30 usr +  0.05 sys =  2.35 CPU)
@ 425531.91/s (n=1000000)
substr_regex_3:  3 wallclock secs ( 2.81 usr +  0.07 sys =  2.88 CPU)
@ 347222.22/s (n=1000000)


Single target:

Benchmark: timing 1000000 iterations of plain_regex, plain_substr,
regex_line, substr_regex_1, substr_regex_3...
plain_regex:  6 wallclock secs ( 3.02 usr +  0.04 sys =  3.06 CPU) @
326797.39/s (n=1000000)
plain_substr:  3 wallclock secs ( 1.78 usr +  0.02 sys =  1.80 CPU) @
555555.56/s (n=1000000)
regex_line:  1 wallclock secs ( 1.54 usr +  0.03 sys =  1.57 CPU) @
636942.68/s (n=1000000)
substr_regex_1:  2 wallclock secs ( 2.20 usr +  0.02 sys =  2.22 CPU)
@ 450450.45/s (n=1000000)
substr_regex_3:  3 wallclock secs ( 2.19 usr +  0.00 sys =  2.19 CPU)
@ 456621.00/s (n=1000000)



What was interesting to me was that although, predictably, the
substring/regex combo was consistently the best performer for the
original match, regexing the whole line was consistently the best
performer when looking for "|N|".  This seems to fly in the face of
the conventional wisdom that substr is faster than m// when you know
what you're looking for and where you're looking for it.

--jay

__BENCH__2__CODE__
#!perl

use strict ;
use warnings ;
use Benchmark qw(:hireswallclock cmpthese timethese);

my $string = '| B  B  B  B |13145551212 B  B  B  B  |N| B  B  B  B  B B | B  
0|001001|001001| 100|10|B|A|' ;

print "\n\nMultiple targets:\n\n" ;

my $results = timethese(10_000_000, {
   'substr_regex_1' => sub { if ( substr( $string, 39, 1 ) =~ /[BNPG]/ ) { my 
$a = $_ } } ,
   'substr_regex_3' => sub { if ( substr( $string, 38, 3 ) =~ /\|[BNPG]\|/ ) { 
my $a = $_ } } ,
   'plain_substr'   => sub { my $sub = substr( $string, 39, 1 ); if (
       $sub eq "B" || $sub eq "N" || $sub eq "P" || $sub eq "G") { my $a = $sub 
} } ,
   'plain_regex'    => sub { if ( $string  =~ /^.{38}\|[BNPG]\|/ ) { my $a = $_ 
} },
   'regex_line'     => sub { if ( $string  =~ /\|[BNPG]\|/ ) { my $a = $_ } },
   'pos_regex_3'    => sub { pos ($string) = 38;
                             if ($string   =~ /\|[BNPG]\|/ ) { my $a = $_ } },
   'pos_regex_1'    => sub { pos ($string) = 39;
                             if ($string   =~ /[BNPG]/ ) { my $a = $_ } } }
) ;
print "\n";
cmpthese $results;
print "\n\nSingle target:\n\n" ;

$results = timethese(10_000_000, {
   'substr_regex_1' => sub { if ( substr( $string, 39, 1 ) =~ /N/ ) { my $a = 
$_ } } ,
   'substr_regex_3' => sub { if ( substr( $string, 38, 3 ) =~ /\|N\|/ ) { my $a 
= $_ } } ,
   'plain_substr'   => sub { if ( substr( $string, 39, 1 ) eq "N" ) { my $a = 
$_ } } ,
   'plain_regex'    => sub { if ( $string  =~ /^.{38}\|N\|/ ) { my $a = $_ } },
   'regex_line'     => sub { if ( $string  =~ /\|N\|/ ) { my $a = $_ } },
   'pos_regex_3'    => sub { pos ($string) = 38;
                             if ($string   =~ /\|N\|/ ) { my $a = $_ } },
   'pos_regex_1'    => sub { pos ($string) = 39;
                             if ($string   =~ /N/ ) { my $a = $_ } } }
) ;
print "\n";
cmpthese $results;

__BENCH__2__RESULTS__

Multiple targets:

Benchmark: timing 10000000 iterations of plain_regex, plain_substr, 
pos_regex_1, pos_regex_3, regex_line, substr_regex_1, substr_regex_3...
plain_regex:    6.26715 wallclock secs ( 6.14 usr +  0.01 sys =  6.16 CPU) @ 
1624431.45/s (n=10000000)
plain_substr:   6.38174 wallclock secs ( 6.31 usr +  0.03 sys =  6.34 CPU) @ 
1576044.13/s (n=10000000)
pos_regex_1:    6.57066 wallclock secs ( 6.56 usr +  0.00 sys =  6.56 CPU) @ 
1523693.43/s (n=10000000)
pos_regex_3:    9.13073 wallclock secs ( 8.97 usr +  0.00 sys =  8.97 CPU) @ 
1115075.83/s (n=10000000)
regex_line:     6.90791 wallclock secs ( 6.80 usr +  0.00 sys =  6.80 CPU) @ 
1471237.31/s (n=10000000)
substr_regex_1: 5.06138 wallclock secs ( 5.09 usr +  0.00 sys =  5.09 CPU) @ 
1963093.84/s (n=10000000)
substr_regex_3: 5.91842 wallclock secs ( 5.91 usr + -0.03 sys =  5.87 CPU) @ 
1702127.66/s (n=10000000)

                   Rate pos_regex_3 regex_line pos_regex_1 plain_substr 
plain_regex substr_regex_3 substr_regex_1
pos_regex_3    1115076/s          --       -24%        -27%         -29%        
-31%           -34%           -43%
regex_line     1471237/s         32%         --         -3%          -7%        
 -9%           -14%           -25%
pos_regex_1    1523693/s         37%         4%          --          -3%        
 -6%           -10%           -22%
plain_substr   1576044/s         41%         7%          3%           --        
 -3%            -7%           -20%
plain_regex    1624431/s         46%        10%          7%           3%        
  --            -5%           -17%
substr_regex_3 1702128/s         53%        16%         12%           8%        
  5%             --           -13%
substr_regex_1 1963094/s         76%        33%         29%          25%        
 21%            15%             --


Single target:

Benchmark: timing 10000000 iterations of plain_regex, plain_substr, 
pos_regex_1, pos_regex_3, regex_line, substr_regex_1, substr_regex_3...
plain_regex:    6.44568 wallclock secs ( 6.39 usr +  0.03 sys =  6.42 CPU) @ 
1557147.31/s (n=10000000)
plain_substr:   3.59761 wallclock secs ( 3.56 usr + -0.00 sys =  3.56 CPU) @ 
2807411.57/s (n=10000000)
pos_regex_1:    8.50115 wallclock secs ( 8.45 usr + -0.02 sys =  8.44 CPU) @ 
1185255.42/s (n=10000000)
pos_regex_3:     6.4463 wallclock secs ( 6.42 usr +  0.00 sys =  6.42 CPU) @ 
1557147.31/s (n=10000000)
regex_line:     3.90568 wallclock secs ( 3.92 usr +  0.00 sys =  3.92 CPU) @ 
2550369.80/s (n=10000000)
substr_regex_1: 4.50092 wallclock secs ( 4.49 usr +  0.00 sys =  4.49 CPU) @ 
2229654.40/s (n=10000000)
substr_regex_3: 4.49159 wallclock secs ( 4.48 usr +  0.00 sys =  4.48 CPU) @ 
2230151.65/s (n=10000000)

                   Rate pos_regex_1 pos_regex_3 plain_regex substr_regex_1 
substr_regex_3 regex_line plain_substr
pos_regex_1    1185255/s          --        -24%        -24%           -47%     
      -47%       -54%         -58%
pos_regex_3    1557147/s         31%          --         -0%           -30%     
      -30%       -39%         -45%
plain_regex    1557147/s         31%          0%          --           -30%     
      -30%       -39%         -45%
substr_regex_1 2229654/s         88%         43%         43%             --     
       -0%       -13%         -21%
substr_regex_3 2230152/s         88%         43%         43%             0%     
        --       -13%         -21%
regex_line     2550370/s        115%         64%         64%            14%     
       14%         --          -9%
plain_substr   2807412/s        137%         80%         80%            26%     
       26%        10%           --

__BENCH__3__CODE__
#!perl

use strict ;
use warnings ;
use Benchmark qw( :hireswallclock cmpthese timethese);

my $string = '| B  B  B  B |13145551212 B  B  B  B  |N| B  B  B  B  B B | B  
0|001001|001001| 100|10|B|A|' ;

print "\n\nMultiple targets:\n\n" ;

my $results = timethese(10_000_000, {
   'substr_regex_1' =>  'if ( substr( $string, 39, 1 ) =~ /[BNPG]/ ) { my $a = 
$_ }' ,
   'substr_regex_3' =>  'if ( substr( $string, 38, 3 ) =~ /\|[BNPG]\|/ ) { my 
$a = $_ }' ,
   'plain_substr'   =>  'my $sub = substr( $string, 39, 1 );
                         if ($sub eq "B" || $sub eq "N" || $sub eq "P" || $sub eq 
"G") { my $a = $sub }' ,
   'plain_regex'    =>  'if ( $string  =~ /^.{38}\|[BNPG]\|/ ) { my $a = $_ }',
   'regex_line'     =>  'if ( $string  =~ /\|[BNPG]\|/ ) { my $a = $_ }',
   'pos_regex_3'    =>  'pos ($string) = 38;
                             if ($string   =~ /\|[BNPG]\|/ ) { my $a = $_ }',
   'pos_regex_1'    =>  'pos ($string) = 39;
                             if ($string   =~ /[BNPG]/ ) { my $a = $_ }' }
) ;
print "\n";
cmpthese $results;
print "\n\nSingle target:\n\n" ;

$results = timethese(10_000_000, {
   'substr_regex_1' =>  'if ( substr( $string, 39, 1 ) =~ /N/ ) { my $a = $_ }' 
,
   'substr_regex_3' =>  'if ( substr( $string, 38, 3 ) =~ /\|N\|/ ) { my $a = 
$_ }' ,
   'plain_substr'   =>  'if ( substr( $string, 39, 1 ) eq "N" ) { my $a = $_ }' 
,
   'plain_regex'    =>  'if ( $string  =~ /^.{38}\|N\|/ ) { my $a = $_ }',
   'regex_line'     =>  'if ( $string  =~ /\|N\|/ ) { my $a = $_ }',
   'pos_regex_3'    =>  'pos ($string) = 38;
                             if ($string   =~ /\|N\|/ ) { my $a = $_ }',
   'pos_regex_1'    =>  'pos ($string) = 39;
                             if ($string   =~ /N/ ) { my $a = $_ }' }
) ;
print "\n";
cmpthese $results;

__BENCH__3__RESULTS__

Multiple targets:

Benchmark: timing 10000000 iterations of plain_regex, plain_substr, 
pos_regex_1, pos_regex_3, regex_line, substr_regex_1, substr_regex_3...
plain_regex:     1.4103 wallclock secs ( 1.41 usr +  0.00 sys =  1.41 CPU) @ 
7107320.54/s (n=10000000)
plain_substr:   5.80651 wallclock secs ( 5.76 usr +  0.00 sys =  5.76 CPU) @ 
1734605.38/s (n=10000000)
pos_regex_1:    3.61992 wallclock secs ( 3.61 usr +  0.00 sys =  3.61 CPU) @ 
2770083.10/s (n=10000000)
pos_regex_3:    3.38102 wallclock secs ( 3.33 usr +  0.00 sys =  3.33 CPU) @ 
3004807.69/s (n=10000000)
regex_line:      1.1214 wallclock secs ( 1.08 usr +  0.00 sys =  1.08 CPU) @ 
9267840.59/s (n=10000000)
substr_regex_1: 1.81725 wallclock secs ( 1.83 usr +  0.00 sys =  1.83 CPU) @ 
5470459.52/s (n=10000000)
substr_regex_3: 1.98296 wallclock secs ( 1.95 usr +  0.00 sys =  1.95 CPU) @ 
5120327.70/s (n=10000000)

                   Rate plain_substr pos_regex_1 pos_regex_3 substr_regex_3 
substr_regex_1 plain_regex regex_line
plain_substr   1734605/s           --        -37%        -42%           -66%    
       -68%        -76%       -81%
pos_regex_1    2770083/s          60%          --         -8%           -46%    
       -49%        -61%       -70%
pos_regex_3    3004808/s          73%          8%          --           -41%    
       -45%        -58%       -68%
substr_regex_3 5120328/s         195%         85%         70%             --    
        -6%        -28%       -45%
substr_regex_1 5470460/s         215%         97%         82%             7%    
         --        -23%       -41%
plain_regex    7107321/s         310%        157%        137%            39%    
        30%          --       -23%
regex_line     9267841/s         434%        235%        208%            81%    
        69%         30%         --


Single target:

Benchmark: timing 10000000 iterations of plain_regex, plain_substr, 
pos_regex_1, pos_regex_3, regex_line, substr_regex_1, substr_regex_3...
plain_regex:    1.06775 wallclock secs ( 1.08 usr +  0.00 sys =  1.08 CPU) @  
9276437.85/s (n=10000000)
plain_substr:   2.04142 wallclock secs ( 2.00 usr +  0.01 sys =  2.02 CPU) @  
4960317.46/s (n=10000000)
pos_regex_1:    3.51549 wallclock secs ( 3.52 usr +  0.02 sys =  3.53 CPU) @  
2831257.08/s (n=10000000)
pos_regex_3:     3.4822 wallclock secs ( 3.48 usr +  0.00 sys =  3.48 CPU) @  
2870264.06/s (n=10000000)
regex_line:    0.906903 wallclock secs ( 0.92 usr +  0.00 sys =  0.92 CPU) @ 
10845986.98/s (n=10000000)
substr_regex_1: 1.99192 wallclock secs ( 1.98 usr +  0.02 sys =  2.00 CPU) @  
5000000.00/s (n=10000000)
substr_regex_3: 2.14122 wallclock secs ( 2.14 usr +  0.00 sys =  2.14 CPU) @  
4672897.20/s (n=10000000)

                    Rate pos_regex_1 pos_regex_3 substr_regex_3 plain_substr 
substr_regex_1 plain_regex regex_line
pos_regex_1     2831257/s          --         -1%           -39%         -43%   
        -43%        -69%       -74%
pos_regex_3     2870264/s          1%          --           -39%         -42%   
        -43%        -69%       -74%
substr_regex_3  4672897/s         65%         63%             --          -6%   
         -7%        -50%       -57%
plain_substr    4960317/s         75%         73%             6%           --   
         -1%        -47%       -54%
substr_regex_1  5000000/s         77%         74%             7%           1%   
          --        -46%       -54%
plain_regex     9276438/s        228%        223%            99%          87%   
         86%          --       -14%
regex_line     10845987/s        283%        278%           132%         119%   
        117%         17%         --

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Reply via email to