http://qs1969.pair.com?node_id=1194601


in reply to Is foreach split Optimized? (Update: No.)

Hi haukex,

You might want to also try using a file handle opened on a reference to your string:

filehandle => sub { my @lines; open my $str_fh, "<", \$str or die "cannot open fh $!"; while (<$str_fh>) { chomp; s/o/i/g; push @lines, $_; } },
This appears to be faster than your other subs, including the one with split (note that I had to change the count value for the result to be meaningful):
$ perl bench_split.pl s/iter regex index split filehandle regex 3.44 -- -9% -36% -48% index 3.13 10% -- -29% -43% split 2.21 56% 42% -- -19% filehandle 1.78 93% 76% 24% --