Your skill will accomplishwhat the force of many cannot PerlMonks

### Re^3: 2*pi*\$r -- constant function without prototype

by almut (Canon)
 on May 01, 2008 at 11:21 UTC Need Help??

Not sure if I would recommend Readonly over constant — at least some people have been bitten by its subtle peculiarities. As an example, see this thread.

BTW, there's also Scalar::Readonly, which is directly manipulating the readonly-flag of scalars (instead of working with tied magic)...

___

Update:  (...it was only a matter of time until someone would post a benchmark, so... :)

```use strict;
use warnings;
use Benchmark 'cmpthese';

use constant NL => "\012";
my \$Nl = "\012";

cmpthese 2000000 => {
constant  => sub { my \$s = "abc".NL."def".NL."ghi"; },
constant2 => sub { my \$s = "abc\${\NL}def\${\NL}ghi"; },
Readonly  => sub { my \$s = "abc\${NL}def\${NL}ghi";   },
ScalarRO  => sub { my \$s = "abc\${nl}def\${nl}ghi";   },
scalarRW  => sub { my \$s = "abc\${Nl}def\${Nl}ghi";   },
direct    => sub { my \$s = "abc\012def\012ghi";     },
};

__END__

Rate  Readonly constant2  scalarRW  ScalarRO  constant
+   direct
Readonly    77851/s        --      -84%      -85%      -85%      -96%
+     -96%
constant2  484262/s      522%        --       -5%       -5%      -75%
+     -75%
scalarRW   510204/s      555%        5%        --       -0%      -74%
+     -74%
ScalarRO   511509/s      557%        6%        0%        --      -74%
+     -74%
constant  1960784/s     2419%      305%      284%      283%        --
+      -0%
direct    1960784/s     2419%      305%      284%      283%        0%
+       --

(as running the benchmark multiple times suggests, differences up to around 5% are insignificant)

___

BTW, I think I remember PBP somewhere saying it's not meant to be treated as a bible...

Replies are listed 'Best First'.
Re^4: 2*pi*\$r -- constant function without prototype
by mscharrer (Hermit) on May 01, 2008 at 21:30 UTC
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.

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://683904]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-22 22:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found