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. :-)

-- Ken


In reply to Re: 'constant' vs array by kcott
in thread 'constant' vs array by fionbarr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.