All,
Here is the benchmark results for all the methods provided so far.
#!/usr/bin/perl use strict; use warnings; use Benchmark 'cmpthese'; my $str = build_str(); cmpthese -5, { by_fh => sub { my @offsets; local $/ = "\n"; open(my $fh, '<', \$str) or die "Unable to use str as fh: $!"; push @offsets, (tell $fh) - 1 while <$fh>; }, by_index => sub { my @offsets; my $p = 0; do { $p = index($str, qq(\n), $p); push @offsets, $p++; } while $p > 0; pop @offsets; }, by_regex1 => sub { my @offsets; $str =~ s/\n/push @offsets, $-[0];"\n"/ge; }, by_regex2 => sub { my @offsets; push @offsets, pos($str) - 1 while $str =~ /\n/g; }, by_regex3 => sub { my @offsets; push @offsets, $-[0] while $str =~ /\n/g; }, }; sub build_str { my ($str, $max) = ('', 200 * 1024); while ( length($str) < $max ) { # Assume lines are between 60-79 characters long $str .= ('#' x ((rand 20) + 60)) . "\n"; } return $str; }
Rate by_regex1 by_regex3 by_fh by_index by_regex2 by_regex1 49.6/s -- -52% -75% -81% -83% by_regex3 103/s 107% -- -48% -61% -66% by_fh 199/s 301% 94% -- -25% -34% by_index 266/s 437% 160% 34% -- -11% by_regex2 300/s 505% 192% 51% 13% --

Cheers - L~R


In reply to Re: Finding the positions of a character in a string by Limbic~Region
in thread Finding the positions of a character in a string by Limbic~Region

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.