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 | |
by jmmitc06 (Beadle) on Oct 04, 2014 at 04:10 UTC | |
by jmmitc06 (Beadle) on Oct 04, 2014 at 19:16 UTC | |
by BrowserUk (Patriarch) on Oct 04, 2014 at 20:15 UTC | |
by jmmitc06 (Beadle) on Oct 05, 2014 at 04:36 UTC | |
by BrowserUk (Patriarch) on Oct 05, 2014 at 15:27 UTC | |
|