Lets see the benchmarks of several alternatives.
use Modern::Perl;
use Benchmark qw'cmpthese';
# set up some test data
my $bar = join "\n", 'a'..'z';
my $qr = qr/\r?\n/;
my $str = "\r?\n";
my $qq = qq/\r?\n/;
my %test = (
' //' => sub{ split( /\r?\n/, $bar ); },
' m//' => sub{ split( m/\r?\n/, $bar ); },
' m""' => sub{ split( m"\r?\n", $bar ); },
' qr//' => sub{ split( qr/\r?\n/, $bar ); },
' qq//' => sub{ split( qq/\r?\n/, $bar ); },
' ""' => sub{ split( "\r?\n", $bar ); },
'$qr ' => sub{ split( $qr, $bar ); },
'$str ' => sub{ split( $str, $bar ); },
'$qq ' => sub{ split( $qq, $bar ); }
);
cmpthese( -5, \%test, 'auto');
Benchmark: running
"", //, m"", m//, qq//, qr//, $qq , $qr , $str
for at least 5 CPU seconds...
"": 6 wallclock secs ( 5.21 usr + 0.02 sys = 5.23 CPU) @ 42325.81/s (n=221364)
//: 6 wallclock secs ( 5.26 usr + 0.00 sys = 5.26 CPU) @ 42626.24/s (n=224214)
m"": 6 wallclock secs ( 5.30 usr + 0.01 sys = 5.31 CPU) @ 42519.96/s (n=225781)
m//: 6 wallclock secs ( 5.20 usr + 0.00 sys = 5.20 CPU) @ 42568.08/s (n=221354)
qq//: 6 wallclock secs ( 5.24 usr + 0.01 sys = 5.25 CPU) @ 42707.43/s (n=224214)
qr//: 6 wallclock secs ( 5.11 usr + 0.03 sys = 5.14 CPU) @ 33277.04/s (n=171044)
$qq : 5 wallclock secs ( 5.15 usr + 0.00 sys = 5.15 CPU) @ 42154.76/s (n=217097)
$qr : 4 wallclock secs ( 5.28 usr + 0.00 sys = 5.28 CPU) @ 39593.94/s (n=209056)
$str : 6 wallclock secs ( 5.29 usr + 0.00 sys = 5.29 CPU) @ 41843.86/s (n=221354)
Rate qr// $qr $str $qq "" m"" m// // qq//
qr// 33277/s -- -16% -20% -21% -21% -22% -22% -22% -22%
$qr 39594/s 19% -- -5% -6% -6% -7% -7% -7% -7%
$str 41844/s 26% 6% -- -1% -1% -2% -2% -2% -2%
$qq 42155/s 27% 6% 1% -- -0% -1% -1% -1% -1%
"" 42326/s 27% 7% 1% 0% -- -0% -1% -1% -1%
m"" 42520/s 28% 7% 2% 1% 0% -- -0% -0% -0%
m// 42568/s 28% 8% 2% 1% 1% 0% -- -0% -0%
// 42626/s 28% 8% 2% 1% 1% 0% 0% -- -0%
qq// 42707/s 28% 8% 2% 1% 1% 0% 0% 0% --
It's worth noting that these are all essentially the same speed, with qr//
showing up to be slightly slower. After running this test several times, qr//
and $qr
were always the slowest, and second slowest, of the bunch. With the others exchanging places regularly.
So basically it doesn't matter how you set up the regex for split()
.