in reply to Re^2: In-place sort with order assignment
in thread In-place sort with order assignment

BTW: If I ever manage to get the keys out of the hash and into an array without blowing memory, then I intend to use your Sort::Key to sort them in-place. But, there doesn't seem to be a an in-place version of keysort that doesn't take a callback?

It seems the interface is not very consistent... actually, the function exists, it is Sort::Key::_sort_inplace.

In any case, perl builtin sort will perform equally faster as @k = sort @k gets optimized by the interpreter into an inplace operation.

BTW, using the quicksort algorithm instead of the default merge sort will also help to reduce memory usage.

Replies are listed 'Best First'.
Re^4: In-place sort with order assignment
by BrowserUk (Patriarch) on Sep 19, 2010 at 19:37 UTC
    @k = sort @k gets optimized by the interpreter into an inplace operation.

    When did that make it into the release? Cos everytime it comes up here, I try it out and it doesn't seem to work. (And my attempt to search for the release info fail.)

    For now, Sort::Key::_sort_inplace() will do me fine.

      That optimization was introduced here in 2004. Probably, the first stable release to incorporate it was perl 5.10.

      update: the optimization is documented in perl5100delta.

        Hm. Do you see anything wrong with this usage that would prevent the in-place optimisation from kicking in?

        perl -E"my @a; $a[$_]=rand for 1..1e6; <>; @a = sort @a; <>"

        Because after the array is populated, memory usage stands at 42mb. After the sort, it jumped to 150MB.

        Which admittedly is substantially better than:

        perl -E"my @a; $a[$_]=rand for 1..1e6; <>; my @b = sort @a; <>"

        Where it leaps from 42MB to 280MB. But it doesn't meet my idea of 'in-place'.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.