> Phil, please see the perlfunc entry for "pos" and the perlre section
> on \G.  This is what you need.
Thanks a lot! I know about pos but thought it was read-only.
And \G is relatively new, isn't it? Certainly wasn't 
existing in '97 when I learned perl :-) 
And the "basics" are seldom read again in the docs...


Thank you very much, although it's still 32% slower:


2505792 bytes to do ...
Benchmark: timing 1000000 iterations of from_start, pos, re_dyn, re_once, substr, 
substr_set...
from_start:  2 wallclock secs ( 1.06 usr +  0.00 sys =  1.06 CPU) @ 943396.23/s 
(n=1000000)
       pos:  0 wallclock secs ( 1.55 usr +  0.01 sys =  1.56 CPU) @ 641025.64/s 
(n=1000000)
    re_dyn:  7 wallclock secs ( 6.13 usr +  0.00 sys =  6.13 CPU) @ 163132.14/s 
(n=1000000)
   re_once:  2 wallclock secs ( 1.22 usr +  0.00 sys =  1.22 CPU) @ 819672.13/s 
(n=1000000)
    substr:  2 wallclock secs ( 2.39 usr +  0.01 sys =  2.40 CPU) @ 416666.67/s 
(n=1000000)
substr_set:  3 wallclock secs ( 3.10 usr +  0.00 sys =  3.10 CPU) @ 322580.65/s 
(n=1000000)
               Rate    re_dyn substr_set    substr       pos  re_once from_start
re_dyn     163132/s        --       -49%      -61%      -75%     -80%       -83%
substr_set 322581/s       98%         --      -23%      -50%     -61%       -66%
substr     416667/s      155%        29%        --      -35%     -49%       -56%
pos        641026/s      293%        99%       54%        --     -22%       -32%
re_once    819672/s      402%       154%       97%       28%       --       -13%
from_start 943396/s      478%       192%      126%       47%      15%         --


Regards,

Phil
#!/usr/bin/perl

use Benchmark qw(cmpthese);


$pos=500;
$runs=1000000;
$_=`cat /etc/* 2> /dev/null`;
study $_;

print length($_), " bytes to do ...\n";

cmpthese($runs,
{
  "from_start"  => sub { m/\S*\s+(\S+)/; },
  "re_dyn"  => sub { m/^[\x00-\xff]{$pos}\S*\s+(\S+)/; },
  "re_once" => sub { m/^[\x00-\xff]{$pos}\S*\s+(\S+)/o; },
  "substr" => sub { substr($_,$pos) =~ m/\S*\s+(\S+)/; },
  "substr_set" => sub { $tmp=substr($_,$pos); $tmp =~ m/\S*\s+(\S+)/; },
  "pos"  => sub { pos($pos); m/\G\S*\s+(\S+)/; },
}
);

Reply via email to