not sure how efficient this is...

Not very much at all :) . It corresponds to wmap in the table below:

Rate wmap count wgrep wregex windex wmap 5.42/s -- -12% -60% -97% -98% count 6.13/s 13% -- -54% -97% -98% wgrep 13.5/s 148% 120% -- -93% -96% wregex 198/s 3558% 3132% 1372% -- -37% windex 317/s 5741% 5062% 2251% 60% --


use strict; use warnings; use Benchmark 'cmpthese'; srand( 0 ); my $s = join '', map chr(97+int(rand(26))), 1..100000; cmpthese( -2, { windex => \&windex, wregex => \&wregex, wgrep => \&wgrep, count => \&count, wmap => \&wmap, } ); sub windex { my @o; my $o = -1; while ( ( $o = index( $s, 'a', $o+1 )) > -1 ) { push @o, $o } return; } sub wregex { my @o; $s =~ m/a(?{ push @o, pos() - 1 })(?!)/; return; } sub wgrep { my @o = grep substr( $s, $_, 1 ) eq 'a', 0..length($s)-1; return; } sub count { my @o; my $count = 0; for ( split //, $s ) { push @o, $count if $_ eq 'a'; ++$count; } return; } sub wmap { my $count = 0; my @o = map { $count++; /a/ ? $count - 1 : () } split //, $s; return; }

the lowliest monk


In reply to Re^2: Producing a list of offsets efficiently by tlm
in thread Producing a list of offsets efficiently by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.