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


in reply to Re^3: 2*pi*$r -- constant function without prototype
in thread 2*pi*$r -- constant function without prototype

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.