This was with version 5.14, using Cygwin. I will try with a more recent version under Windows.
Update This very strange: running the exact same program on the same computer but under a pure Windows setting and perl 5, version 24, subversion 1 (v5.24.1) and from a bash console, I obtain awfully bad results for the file handle solution:
s/iter filehandle regex index split
filehandle 100 -- -97% -97% -98%
regex 3.50 2758% -- -14% -42%
index 3.02 3217% 16% -- -33%
split 2.02 4868% 74% 50% --
I can only suspect that there might be something wrong with the management of Windows end-of-line pairs of characters.
Still under Windows, same program, from a dos windows, with version v5.16.3:
s/iter regex index split filehandle
regex 3.69 -- -2% -29% -39%
index 3.63 2% -- -27% -38%
split 2.63 40% 38% -- -15%
filehandle 2.25 64% 61% 17% --
I also tried (again on Cygwin with Perl version v5.14.4) this additional sub:
filehandle2 => sub {
open my $str_fh, "<", \$str or die "cannot open fh $!";
my @lines = <$str_fh>;
for (@lines) {
chomp;
s/o/i/g;
;
}
},
but the result is not so good as the first filehandle solution (but still better than the others):
$ perl bench_split.pl
s/iter regex index split filehandle2 fi
+lehandle
regex 3.44 -- -10% -35% -45%
+ -48%
index 3.11 11% -- -28% -39%
+ -42%
split 2.24 53% 39% -- -16%
+ -20%
filehandle2 1.89 82% 65% 19% --
+ -5%
filehandle 1.80 92% 73% 25% 5%
+ --
Finally, I also tried this solution with a map:
filehandle3 => sub {
open my $str_fh, "<", \$str or die "cannot open fh $!";
my @lines = map { chomp; s/o/i/g } <$str_fh>;
},
and thought it might be faster, but this turns out to be slower than all the other solutions:
$ perl bench_split.pl
s/iter filehandle3 regex index split fi
+lehandle
filehandle3 3.64 -- -7% -16% -39%
+ -51%
regex 3.38 8% -- -9% -34%
+ -47%
index 3.07 18% 10% -- -28%
+ -42%
split 2.22 64% 52% 38% --
+ -20%
filehandle 1.77 105% 90% 73% 25%
+ --
|