G'day fionbarr,
"I've been thinking about replacing the array definition with the 'constant' definition but a little reading (on google) about using constants seems to indicate that is very little speed improvement. Opinions?"
You'd probably get a better answer if you linked to the articles you've been reading and specified what array operations you were comparing. You've really provided nothing on which to base an opinion.
Here's a fact though (from constant - CAVEATS):
"In the current version of Perl, list constants are not inlined ..."
[At the time of writing, that link is for v5.18.0]
Here's timings for accessing the first element (using v5.18.0):
$ perl -Mstrict -Mwarnings -E '
use Time::HiRes;
use constant LABEL_ARRAY => qw(Title Section Subsection Class Cate
+gory Degree Attempt CountOfCounts);
my @label_array = qw(Title Section Subsection Class Category Degre
+e Attempt CountOfCounts);
my $x;
my $t1 = Time::HiRes::time;
$x = (LABEL_ARRAY)[0] for 1 .. 1_000_000;
my $t2 = Time::HiRes::time;
say $t2 - $t1;
my $t3 = Time::HiRes::time;
$x = (@label_array)[0] for 1 .. 1_000_000;
my $t4 = Time::HiRes::time;
say $t4 - $t3;
'
2.28321886062622
0.133409023284912
So, in that context, there is quite the opposite of any sort of speed improvement. Of course, while you have to access the constant array element like that (i.e. (CONSTANT)[$index]), you're more likely to access a lexical array element like this: $array[$index]
$ perl -Mstrict -Mwarnings -E '
use Time::HiRes;
use constant LABEL_ARRAY => qw(Title Section Subsection Class Cate
+gory Degree Attempt CountOfCounts);
my @label_array = qw(Title Section Subsection Class Category Degre
+e Attempt CountOfCounts);
my $x;
my $t1 = Time::HiRes::time;
$x = (LABEL_ARRAY)[0] for 1 .. 1_000_000;
my $t2 = Time::HiRes::time;
say $t2 - $t1;
my $t3 = Time::HiRes::time;
$x = $label_array[0] for 1 .. 1_000_000;
my $t4 = Time::HiRes::time;
say $t4 - $t3;
'
2.29306197166443
0.0892441272735596
So, perhaps I do have an opinion on these articles that indicate "very little speed improvement";
and that opinion would be that they are wrong. :-)
|