in reply to Re^2: Optimizing a double loop
in thread Optimizing a double loop
You can still generate the full array of counts afterwards and have a speed benefit, because you only have to iterate once over the big array (as opposed to once per overlap in your current implementation).
Update:
Example code for obtaining the full array:
use strict; use warnings; my @a = (10, 15, -20, -30); my $highest = abs($a[-1]); my @full = (0) x ($highest + 1); # decrement all negative values by 1, otherwise # we would exclude the end points: for (@a) { $_-- if $_ < 0; } my $count = 0; for (0..$highest) { if (abs $a[0] == $_) { my $c = shift @a; if ($c > 0) { $count++; } else { $count--; } } $full[$_] += $count; } print join('', @full), "\n";
This assumes that there are never two values of same magnitude in the array, ie never exactly matching start- and endpoints. You can generalize it on your own :-)
Second update: I've just realized that the extension is very simple:
Instead of if (abs $a[0] == $_) write while (abs $a[0] == $_), and you're done.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: Optimizing a double loop
by roibrodo (Sexton) on Jun 03, 2010 at 12:51 UTC | |
by moritz (Cardinal) on Jun 03, 2010 at 14:02 UTC | |
by roibrodo (Sexton) on Jun 03, 2010 at 14:17 UTC | |
by BrowserUk (Patriarch) on Jun 03, 2010 at 22:39 UTC |
In Section
Seekers of Perl Wisdom