jmmitc06 has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Dereferencing Performance - Confused by Results
by Athanasius (Cardinal) on Oct 04, 2014 at 03:45 UTC

    Hello jmmitc06,

    The syntax:

    @smrow = @{$sm_rows[$_]};

    doesn’t just perform a dereference, it also copies the contents of the right-hand side into @smrow. So you have to balance the additional overhead of making copies against the saving on dereferences within the loop.

    Update: Perhaps you should investigate the Data::Alias module on . For example, you can replace @smrow = @{$sm_rows[$_]}; with:

    use Data::Alias; ... alias @smrow = @{$sm_rows[$_]};

    which avoids any copying of the data.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Thanks, I knew that some time ago but I didn't think about it, that makes much more sense then.

      Thanks, the Alias module really made a big difference. It gives some strange errors in my multiprocessed code (using threads):

       "thread x terminated abnormally: can't use an undefined value as a hash reference at [program_name] line [line with alias]", <[file I'm opening]> [random line]

      It must be something involving the order in which alias and threads get around to doing that voodoo they do so well, because sometimes it works fine other times it gives errors. Oh well, it's a cool module I think I'll find many uses for it and it makes it fast enough that multiprocessing isn't really necessary.

        It gives some strange errors in my multiprocessed code (using threads):

        Aliasing won't work across threads; you'd need to use threads::shared data.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.