in reply to Re^4: Howto Avoid Memory Problem in List::MoreUtils
in thread Howto Avoid Memory Problem in List::MoreUtils
Yes, inside perl internals, "inplace" just means that instead of passing the elements on the stack, a reference to the target array is passed and the sorting is done directly over it.
... but if the quicksort algorithm is selected, @a = sort @a is also an in-place sort as described on the article pointed by you (if we ignore degenerated cases).
If you need 1.4GB to sort 400MB, it means that you are probably converting numbers to strings inside the sort. For instance:
... and as you can see, there is a simple workaround (though it would make the sorting much slower).use Memchmark 'cmpthese'; use sort '_quicksort'; cmpthese( none => sub { my @a = map { int rand 100 } 1..1000000; }, sort_num => sub { my @a = map { int rand 100 } 1..1000000; @a = sort { $a<=> $b } @a }, sort_str => sub { my @a = map { int rand 100 } 1..1000000; @a = sort { $a cmp $b } @a }, sort_str_workaround => sub { my @a = map { int rand 100 } 1..1000000; my ($c, $d); @a = sort { ($c, $d) = ($a,$b); $c cmp $d } @a } ); __END__ test: none, memory used: 3997696 bytes test: sort_num, memory used: 3997696 bytes test: sort_str, memory used: 68202496 bytes test: sort_str_workaround, memory used: 3997696 bytes
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Howto Avoid Memory Problem in List::MoreUtils
by BrowserUk (Patriarch) on May 04, 2006 at 12:30 UTC | |
by salva (Canon) on May 04, 2006 at 13:18 UTC | |
by BrowserUk (Patriarch) on May 04, 2006 at 13:42 UTC | |
by salva (Canon) on May 04, 2006 at 14:07 UTC | |
by BrowserUk (Patriarch) on May 04, 2006 at 14:40 UTC |