Interestingly, doing that for the simple case seems to actually impede rather than help performance:

#!/usr/bin/perl use strict; use warnings; use Benchmark qw( cmpthese ); my @bogus; $bogus[ 1 ]{ foo }[ 2 ]{ bar }[ 3 ]{ baz } = 5; $bogus[ 1 ]{ foo }[ 2 ]{ bar }[ 3 ]{ quux } = 6; our @bogus2; $bogus2[ 1 ]{ foo }[ 2 ]{ bar }[ 3 ]{ baz } = 5; $bogus2[ 1 ]{ foo }[ 2 ]{ bar }[ 3 ]{ quux } = 6; cmpthese( -3 => { without_common => sub { my $prod = $bogus[ 1 ]{ foo }[ 2 ]{ bar }[ 3 ]{ baz } * $bogus[ 1 ]{ foo }[ 2 ]{ bar }[ 3 ]{ quux }; }, with_common => sub { my $common = $bogus[ 1 ]{ foo }[ 2 ]{ bar }[ 3 ]; my $prod = $common->{ baz } * $common->{ quux }; }, with_for => sub { my $prod; $prod = $_->{ baz } * $_->{ quux } for $bogus[ 1 ]{ foo }[ 2 ] +{ bar }[ 3 ]; }, with_for_g => sub { my $prod; $prod = $_->{ baz } * $_->{ quux } for $bogus2[ 1 ]{ foo }[ 2 +]{ bar }[ 3 ]; }, without_common_g => sub { my $prod = $bogus2[ 1 ]{ foo }[ 2 ]{ bar }[ 3 ]{ baz } * $bogus2[ 1 ]{ foo }[ 2 ]{ bar }[ 3 ]{ quux }; }, with_common_g => sub { my $common = $bogus2[ 1 ]{ foo }[ 2 ]{ bar }[ 3 ]; my $prod = $common->{ baz } * $common->{ quux }; }, } ); __END__ Rate with_for with_for_g without_common without_c +ommon_g with_common with_common_g with_for 346092/s -- -1% -17% + -19% -36% -37% with_for_g 350487/s 1% -- -16% + -18% -35% -37% without_common 416843/s 20% 19% -- + -3% -23% -25% without_common_g 429491/s 24% 23% 3% + -- -20% -22% with_common 537863/s 55% 53% 29% + 25% -- -3% with_common_g 553408/s 60% 58% 33% + 29% 3% --

I have to say this result surprised me.

Makeshifts last the longest.


In reply to Re^2: Perl and common subexpressions by Aristotle
in thread Perl and common subexpressions by Stevie-O

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.