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.
I had the idea to use Data::Dumper with Deparse = 1 on the hash to look how perl optimizes the constants:
$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"; }
};
(I removed the
use warnings; use strict 'refs'; from each sub to make it easier to read).
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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.