note
mscharrer
The benchmark results vary quite much on my laptop (Perl 5.8.8, Ubuntu). Especially 'constant' and 'direct' are not identical but differ by a factor of 1/2 to 2.<p>
I had the idea to use Data::Dumper with Deparse = 1 on the hash to look how perl optimizes the constants:
<code>
$VAR1 = {
'direct' => sub { my $s = "abc\ndef\nghi"; },
'scalarRW' => sub { my $s = "abc${Nl}def${Nl}ghi"; },
'constant' => sub { my $s = "abc\ndef\nghi"; },
'Readonly' => sub { my $s = "abc${NL}def${NL}ghi"; },
'constant2' => sub { my $s = qq[abc${\"\n";}def${\"\n";}ghi]; },
'ScalarRO' => sub { my $s = "abc${nl}def${nl}ghi"; }
};
</code>
(I removed the <c>use warnings; use strict 'refs';</c> from each sub to make it easier to read).<br>
As you can see 'constant' is identical with 'direct' after compilation. 'constant2' still has the dereferencing and referencing included, which makes it slow. The others have the variable included and only differ on how it is accessed. Because Readonly is tied it is the slowest.
683898
683904