in reply to Re^6: Howto Avoid Memory Problem in List::MoreUtils
in thread Howto Avoid Memory Problem in List::MoreUtils
but if you are sorting lexicograficaly, you can not compare numbers as numbers because they are not equivalent operations (i.e.: 2 < 10 but 2 gt 10).
It shouldn't be too difficult to write a sorting sub in XS that uses a comparison callback equivalent to the Perl {($c, $d) = ($a, $b); $c cmp $d }. But converting the numbers to strings is an expensive operation, even in C.
Also, you can apply a transformation to the numbers to make them sort lexicograficaly when compared as numbers. For instance, for positive integers with 8 or less digits (untested):
use Sort::Key 'ukeysort_inplace'; my $digits = 8; ukeysort_inplace { my $a = int $_; length $a > $digits and die 'integer $a is too big'; $a .= ' ' x ($digits - length $a); $a =~ tr/ 0123456789/0123456789a/; hex $a } @array;
(usortkey_inplace should not use more than 8 additional bytes per value on a 32bits machine, update: + 4 bytes for the merge phase of the mergesort)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^8: Howto Avoid Memory Problem in List::MoreUtils
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 |