in reply to Finding the positions of a character in a string

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

Replies are listed 'Best First'.
Re^2: Finding the positions of a character in a string
by Roy Johnson (Monsignor) on Nov 29, 2005 at 19:40 UTC
    I found this to be about 30% faster than by_index and something over 10% faster than by_regex2
    by_index2 => sub { my @offsets; for (my $p = 0; ($p = index($str, "\n", $p)) > 0; push @offset +s, $p++) { } \@offsets; # I made all of them return a ref to the array so I + could check results },

    Caution: Contents may have been coded under pressure.