Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^4: 2*pi*$r -- constant function without prototype

by mscharrer (Hermit)
on May 01, 2008 at 21:30 UTC ( #684006=note: print w/replies, xml ) Need Help??

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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://684006]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2023-11-28 20:11 GMT
Find Nodes?
    Voting Booth?

    No recent polls found