Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
This should perhaps go ino a different section - and if a sufficiently blessed personage decides to move if then I have no problem with that

I have been thinking about the famed Schwartzian Transform as it is coming in extremely handy for some stuff I want to do. However, as seems to have been noted elsewhere*, one problem with the ST is that it is not exactly memory efficient in that you end up with a number of temporary copies of the array floating around requiring garbage collection. This is merely inconvenient for small arrays and small elements - but if you are sorting (as I am) large arrays of large complex structures (the results of XML::Simpled:XMLin() if you must know), then this can cause exciting disk swaps (and since I'm testing this on a windows PC even more exciting crashes when something fails to handle failed malloc()s properly) which rather defeats the point of the ST.

Once upon a time, I learned APL - a language which could perhaps be described as perl-like only terser - and APL has a built in array sort mchanism. The interesting thing about APL's sort routine (⍋ or ⍒ aka Del(ta) Stile) is that it returns you an array of indices into the original array rather than the sorted array itself. Thus, instead of doing (in perl notation)

my @sortedarray = sort @unsortedarray;
you actually do
my @sortedarray = @unsortedarray[sort @unsortedarray];
It occured to me that this idea could be used in the ST so that what gets passed around in the map{} sort{} map{} is in fact [$sort_target, $index] rather than [$sort_target, $entire_element] as this is much much smaller.

The code to do this is below and seems to both use less memory and be slightly quicker although I have yet to run this on a truly huge set of data

my @sorted = @unsorted[ map {$$_[1]} sort {$$b[0]<=>$$a[0]} map [expensive($unsorted[$_]), $_]; } 0..$#unsorted ];
Additional thought: (Inspired by diotalevi) There are probably a number of cases where you actually want to make further manipulations to the index to the data rather than the data itself. In such cases, the  @unsorted[ ] may be omitted.

Comments, (constructive) abuse, praise etc. welcome...

Dingus
*There is almost certainly a better discussion - (update) some others are noted in the replies below
(update misc typos fixed)


Enter any 47-digit prime number to continue.

In reply to An APL trick for the Schwartzian Transform by dingus

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-25 10:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found