kennethk has asked for the wisdom of the Perl Monks concerning the following question:
After reading Shift, Pop, Unshift and Push with Impunity!, a question occurred to me (purely academic). If I have a long array and my goal is to perform some test on each element and remove those elements that fail, what are the best ways to do it from CPU and memory standpoints? So one choice would be
@array = grep(!/^\#/, @array)
where presumably the grep operation has been heavily optimized for CPU time. However, this should create a temporary result array, which in turn could double my memory footprint. On the other extreme of the spectrum, I could say
for ( reverse 0 .. $#array) { splice (@array,$_,1) if ($array[$_] =~ /^\#/) }
but unless just about every entry is excised, that imposes a large performance toward the end of the operation. So the above-noted node inspired the following solution
for (0 .. $#array) { push @array, $value if ($value = shift @array) !~ /^\#/ }
My question is: would this ultimately have worse memory penalties than grep? It seems it must ultimately allocate enough memory at one end to accommodate the entire set, and then the interpreter/system cannot recover this memory since the variable is still allocated. Also, is there an even more clever way to do this I'm missing?
Update:Following the notes by johngg and jwkrahn, I've corrected an error in my splice loop and learned something new about negative indices. Just because you tested code doesn't mean it did what you thought...
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Efficient array element deletion
by ikegami (Patriarch) on Dec 04, 2008 at 23:10 UTC | |
by kennethk (Abbot) on Dec 04, 2008 at 23:39 UTC | |
by ikegami (Patriarch) on Dec 05, 2008 at 02:22 UTC | |
Re: Efficient array element deletion
by johngg (Canon) on Dec 04, 2008 at 23:13 UTC | |
Re: Efficient array element deletion
by jwkrahn (Abbot) on Dec 04, 2008 at 23:17 UTC | |
by ikegami (Patriarch) on Dec 04, 2008 at 23:55 UTC | |
by johngg (Canon) on Dec 05, 2008 at 12:58 UTC | |
by ikegami (Patriarch) on Dec 05, 2008 at 13:07 UTC | |
by BrowserUk (Patriarch) on Dec 05, 2008 at 14:11 UTC | |
| |
Re: Efficient array element deletion
by ikegami (Patriarch) on Dec 04, 2008 at 22:55 UTC | |
Re: Efficient array element deletion
by fert (Acolyte) on Dec 04, 2008 at 23:12 UTC | |
by GrandFather (Saint) on Dec 04, 2008 at 23:36 UTC | |
Re: Efficient array element deletion
by Sinister (Friar) on Dec 05, 2008 at 07:50 UTC |