There is no way to use the perl's built-in sort to achieve your goal.

If your storing 32-bit integers in a string, then you effectively have a C-style array of ints. It would therefore be fairly easy (assuming that you compile your own perl) to use Inline::C to get at the base address of that C-style array, ie. the base address of the scalar, and use your C-RTL's qsort() routine to do the sorting. This would be far quicker than any Perl solution.

If you use a binary distribution of perl, then it would still be possible, though awkward, to use the C-RTL qsort(). Getting the base address of the scalar can be done with pack, and (assuming AS perl), it should be possible to use Win32::API to get the entrypoint for qsort(). The problem then would be providing a callback address for the comparison function. Possibly possible :), but awkward and fragile. Mapping the pointers the qsort function gives to the compare function from one string to valid pointers into another would be possible, but messy.

I had a play with implementing a pure perl qsort using vec to get at the individual integers. It works, and could be used to sort one string based upon the contents of another (although I'm not entirely sure I understand what you mean by that), but it wouldn't be quick by any stretch of the imagination.

I tried it on 100,000 ints and it took a little under 5 minutes, which is an order of magnitude slower than using sort on a standard array. For contrast, the C-RTL qsort() function will do 1,000,000 in 4 seconds. I'm afraid that this is one of those things that there is simply no substitute for getting closer to the metal and using C or assember.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Hooray!
Wanted!


In reply to Re: Re: Re: Re: Re: sorting a vec by BrowserUk
in thread sorting a vec by mcinnes

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.