in reply to Re^2: Style: buried variables or double referencing?
in thread Style: buried variables or double referencing?

Depends. Readonly is slow, and not everyone can use Readonly::XS. The following code takes 48 seconds:

use Readonly; Readonly $C1 => 1; my $t0 = time(); my $a = 2; for (1 .. 10000000) { if ($a ++ == $C1) { ; } } print time() - $t0;

When this takes 4 second:

use constant C1 => 1; my $t0 = time(); my $a = 2; for (1 .. 10000000) { if ($a ++ == C1) { ; } } print time() - $t0;

If performance is important, use constant.

Replies are listed 'Best First'.
Re^4: Style: buried variables or double referencing?
by tilly (Archbishop) on Aug 21, 2005 at 05:06 UTC
    I'd be happier developing with a function that called Readonly in development and then something like this in production:
    sub fake_readonly { $_[0] = $_[1]; }
    And now you get most of the advantages of Readonly, but performance is not a problem. (I don't like constant because I'm a big fan of interpolating, and I often have to check whether someone has a constant or function because I'm afraid that it will try to suck up arguments.)
Re^4: Style: buried variables or double referencing?
by Anonymous Monk on Aug 22, 2005 at 09:58 UTC
    That's a difference of 4.4 microseconds. That's a difference I'm willing to take in most of my programs.