![]() |
|
The stupid question is the question not asked | |
PerlMonks |
An APL trick for the Schwartzian Transformby dingus (Friar) |
on Nov 14, 2002 at 13:15 UTC ( #212830=perlmeditation: 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) you actually do 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 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 Enter any 47-digit prime number to continue.
Back to
Meditations
|
|