So I have a particular piece of code (shown below) that is part of a project that I like to work on improving in my spare time. Recently I was playing with trying to dereference more efficiently AKA don't repeatedly dereference the same variable in the same loop, do it once before the loop. However, after implementing this change on this example it is actually slower, I would have thought at the very least it would be as fast. Example 1 is the faster pre modification code, example 2 is the slower post modification. I haven't posted the whole script as it is large but I can if anyone wants it.

#EXAMPLE 1 OUTER: { for (@indices) { $smrow = $sm_rows[$_]; #sm_rows is an array of arrays #LINE 1 $omrow = $sm_rows[$mapping[$_]]; #mapping is a 1D vector of intege +rs #LINE 2 for (@reverse) { if ( $$smrow[$_] != $$omrow[$mapping[$_] ) #LINE 3 { last OUTER; } } } } #EXAMPLE 2 OUTER: { for (@indices) { @smrow = @{$sm_rows[$_]}; #sm_rows is an array of arrays #LINE 1 @omrow = @{$sm_rows[$mapping[$_]]}; #mapping is a 1D vector of int +egers #LINE 2 for (@reverse) { if ( $smrow[$_] != $omrow[$mapping[$_] ) #LINE 3 { last OUTER; } } } }

I profiled with NYTProf and for example 1 Line 1,2 and 3 (see comments in examples) required 195us, 207us and 16.8ms respectively. For example 2, lines 1,2 and 3 required 4.87ms, 4.79ms and 16.8ms, I would have though example 2 would save dereferencing on LINE 3 multiple times and save a decent percentage of time but it does not. I have repeated the profiling enough to feel certain it's not just test to test variation in runtime. I actually don't care about it being faster, it's plenty fast enough, I'm just curious why dereferencing before the loop and operating on arrays was slower than working with the array refs directly, so any explanation for this would be insightful. It just went against my intuition so I must be misunderstanding something or perl is doing something I'm not considering.

Thanks for all your help!

UPDATE: Sleeping each thread until they have all been created solved the problem with the multiprocessed version. Threads are a magical thing


In reply to Dereferencing Performance - Confused by Results by jmmitc06

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.