Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

(tye)Re: The Ovidian Transform

by tye (Cardinal)
on Dec 31, 2001 at 21:28 UTC ( #135392=note: print w/ replies, xml ) Need Help??


in reply to The Ovidian Transform

There are lots of ways to get about a 50% speed increase over an ST. Creating all of those tiny anonymous arrays isn't a speedy task. It does lead to rather simple and non-error-prone code, however.

One alternative that is as general purpose is to build a separate array of keys to sort on and then sort a list of indices from that:

my @sorted= do { my @sorton= map { get_date($_) } @data; my @indices= sort { $sorton[$a] cmp $sorton[$b] } 0..@data; @data[@indices]; };
Another alternative that can be slightly faster but is a bit harder to customize is to combine the key to sort on with the original data into a single string in a reversible manner and use:
my @sorted= map { restore_orig_data($_) } sort map { prepend_sort_key($_) } @data;
See also http://www.sysarch.com/perl/sort_paper.html or even http://www.perl.com/doc/FMTEYEWTK/sort.html.

        - tye (but my friends call me "Tye")


Comment on (tye)Re: The Ovidian Transform
Select or Download Code
Re(2): (tye): The Ovidian Transform
by dmmiller2k (Chaplain) on Dec 31, 2001 at 23:21 UTC

    Good one, tye. I've had great success with your second alternative, prepending the sort key to the original data, making a single string and sorting that.

    It has been at *least* 50% faster than the "standard" ST technique, in the instances where I've used it (your mileage may vary, of course :). Mostly, those instances were cases where the data was complex enough that sorting was slow to begin with...

    Update: While this approach probably cannot compete with the raw speed of C, the cost-benefit ratio is far more favorable (to me), given the size of the learning curve I would require in order to be effective using XS (which is essentially what using C in this context boils down to).

    dmm

    You can give a man a fish and feed him for a day ...
    Or, you can
    teach him to fish and feed him for a lifetime

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://135392]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (11)
As of 2014-12-21 15:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (106 votes), past polls