Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Producing a list of offsets efficiently

by tlm (Prior)
on May 28, 2005 at 20:31 UTC ( #461398=note: print w/replies, xml ) Need Help??

in reply to Producing a list of offsets efficiently

index beats everything else I have tried, including various regexps. The fastest I have found is

my @o; my $o = -1; push @o, $o while ($o = index($s, 'a', $o+1)) > -1;


use strict; use warnings; use Benchmark 'cmpthese'; srand( 0 ); my $s = join '', map chr(97+int(rand(26))), 1..100_000; cmpthese( -1, { windex => \&windex, windex_1 => \&windex_1, wregex => \&wregex, } ); sub windex { my @o; my $o = -1; while ( ( $o = index( $s, 'a', $o+1 )) > -1 ) { push @o, $o } return; } sub windex_1 { my @o; my $o = -1; push @o, $o while ($o = index($s, 'a', $o+1)) > -1; return; } sub wregex { my @o; $s =~ m/a(?{ push @o, pos() - 1 })(?!)/; return; } __END__ Rate wregex windex windex_1 wregex 204/s -- -37% -39% windex 324/s 59% -- -4% windex_1 336/s 65% 4% --

Updates: Way too many. Reversed my first reversal (I'd inadvertently introduced a bug in wregex that resulted in only one match taking place, which made it look faster). Minor tweaks to wregex (eliminated the unneccessary /g, and removed a stray 0; in the (?{}) code.

the lowliest monk

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2020-06-06 08:00 GMT
Find Nodes?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?

    Results (41 votes). Check out past polls.