More on constants in perl

So, the thing that always happens happened. When you post something about which modules you use, or how the behave, perl folks come out of the woodwork to tell you all about how you got it wrong some new module you should be using, or how awesome some other module is.

Mind you, this is one of the greatest things about the perl community in general. Y’all sure are a vocal bunch. So, in particular I was told:

  1. I don’t understand constant folding. Which, whether true or not, isn’t really relevant for my original point, which was “golly this behavior is unexpected and surprising”
  2. I should be using Const::Fast, because it is better than Readonly.

So, here’s some code I wrote up:

#!/usr/bin/env perl
use warnings;
use strict;
use Dumbbench;
use Readonly;
use Const::Fast;
use Data::Dumper;
$Data::Dumper::Deparse = 1;
use constant USE_CONSTANT_KEY => "key";
use constant USE_CONSTANT_VALUE => "value";
Readonly my $READONLY_KEY => "key";
Readonly my $READONLY_VALUE => "value";
const my $CONST_FAST_KEY => "key";
const my $CONST_FAST_VALUE => "value";
my $bench = Dumbbench->new(
target_rel_precision => 0.005, # seek ~0.5%
initial_runs => 1000, # the higher the more reliable
);
my %test_hash = ( key => "value" );
my %subs = (
use_constant => sub {
my $i = 0;
if (defined $test_hash{USE_CONSTANT_KEY()}) {
if ($test_hash{USE_CONSTANT_KEY()} eq USE_CONSTANT_VALUE) {
$i++;
}
}
},
readonly => sub {
my $i = 0;
if (defined $test_hash{$READONLY_KEY}) {
if ($test_hash{$READONLY_KEY} eq $READONLY_VALUE) {
$i++;
}
}
},
const_fast => sub {
my $i = 0;
if (defined $test_hash{$CONST_FAST_KEY}) {
if ($test_hash{$CONST_FAST_KEY} eq $CONST_FAST_VALUE) {
$i++;
}
}
},
);
warn Dumper(\%subs);
$bench->add_instances(
Dumbbench::Instance::PerlSub->new(name => 'use_constant', code => $subs{use_constant}),
Dumbbench::Instance::PerlSub->new(name => 'readonly', code => $subs{readonly}),
Dumbbench::Instance::PerlSub->new(name => 'const_fast', code => $subs{const_fast}),
);
$bench->run;
$bench->report;
view raw constant_shootout.pl hosted with ❤ by GitHub

It basically has 3 subs, one for constant, one for Readonly, and one for Const::Fast. It Dumper’s and Deparses the code (so we can see what awesome constant folding is happening (and it is awesome.)) It then uses the lovely Dumbbench module to benchmark them and see the actual difference. Here are the results:

$VAR1 = {
'readonly' => sub {
use warnings;
use strict 'refs';
my $i = 0;
if (defined $test_hash{$READONLY_KEY}) {
if ($test_hash{$READONLY_KEY} eq $READONLY_VALUE) {
$i++;
}
}
},
'use_constant' => sub {
use warnings;
use strict 'refs';
my $i = 0;
if (defined $test_hash{'key'}) {
if ($test_hash{'key'} eq 'value') {
$i++;
}
}
},
'const_fast' => sub {
use warnings;
use strict 'refs';
my $i = 0;
if (defined $test_hash{$CONST_FAST_KEY}) {
if ($test_hash{$CONST_FAST_KEY} eq $CONST_FAST_VALUE) {
$i++;
}
}
}
};
use_constant: Ran 1214 iterations (189 outliers).
use_constant: Rounded run time per iteration: 4.6662e-07 +/- 1.2e-10 (0.0%)
readonly: Ran 1214 iterations (186 outliers).
readonly: Rounded run time per iteration: 4.7046e-06 +/- 2.1e-09 (0.0%)
const_fast: Ran 1546 iterations (528 outliers).
const_fast: Rounded run time per iteration: 4.6802e-07 +/- 1.2e-10 (0.0%)
view raw results.pl hosted with ❤ by GitHub

So, I can’t really see a massive performance difference or compelling difference. Mind you, there *is* a performance difference, but like, we’re talking microseconds here right? I understand TIMTOWTDI. I ❤ that about perl. This time, I simply don’t get the benefits.

UPDATE: Holy crap, some nice commenter fellow sent me a link to a page in which someone compared 21 ways of doing constants in perl on CPAN. 21. There are 21 different solutions on how to do constants on CPAN. What a grim, meathook future.

This entry was posted in tech and tagged , , . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s