Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

•Re^6: Benchmark, -s versus schwartzian

by merlyn (Sage)
on Aug 23, 2004 at 19:28 UTC ( [id://385192]=note: print w/replies, xml ) Need Help??


in reply to Re^5: Benchmark, -s versus schwartzian
in thread Benchmark, -s versus schwartzian

You can always do the GRT by using only the key...
Uh, that's sometimes the hard part. Suppose you wanted to sort by a floating point number (say, age in days), then descending by a string of varying length that might have a NUL in it, and then descending by another integer (byte size). Quick, construct the GRT for that. Not easy, huh. Trivial in the ST.

Yes, you can always get a single GRT string for a multilevel sort. But sometimes, as I said, it takes a frickin' genius.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

  • Comment on •Re^6: Benchmark, -s versus schwartzian

Replies are listed 'Best First'.
Re^7: Benchmark, -s versus schwartzian
by fizbin (Chaplain) on Aug 25, 2004 at 20:09 UTC
    You know, all this fancy fast sorting would be much, much easier if perl's sort function compared references to arrays the way python's sort function does: that is, lexographically. (aka "dictionary order") Then, the standard Schwartzian transform could omit the { $a->[0] cmp $b->[0] or $a->[1] cmp $b->[1] } bit, and the performance differences between the ST and GRT would almost vanish. This would allow us to stop trying to construct a GRT for squeezing that extra 2% out of a long sort time and get on with our lives.

    Actually, is there a good reason why the builtin sort compare doesn't compare references to arrays in this fashion?

    -- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

      s/2%/50%/ (roughly, depending). And it isn't just the comparison routine that is the problem. Constructing a huge number of tiny arrays takes some time (and memory).

      I think fast, flexible, stable sort can be rolled into a module that makes the key construction easy and natural where the module overhead is low and outside of the sorting and so using the module would give performance (in speed and memory use) on par with a very efficient hand-rolled solution and much faster than any general-purpose Perl sorting modules I've looked at.

      Having sort default to sorting array references as you've specified certainly makes sense. The boring answer is that backward compatibility prevents it from happening in Perl5. I won't pretend to remember how this type of operation is likely to behave in Perl6.

      - tye        

        Okay, fair enough - the allocation of all those arrays would probably hurt severely. Much better to do the sort-the-indexes trick you mention elsewhere in this thread, or the GRT variant mentioned in fast, flexible, stable sort.

        But as for backwards compatibility - are you sure? Isn't the sorting of array references at this point essentially a random shuffle? (Meaning that it is unspecified, and could be different each time you run perl, not that it can be used in a situation where you actually need a random shuffle) Couldn't some order therefore be imposed upon it?

        -- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/
Re^7: Benchmark, -s versus schwartzian
by ambrus (Abbot) on Aug 23, 2004 at 19:41 UTC

    If sort {$a<=>$b} is indeed optimized (it seems so), you can just use it for a floating-point key, you just have to 'no warnings "numeric"; and put the float in the first place.

    You are, however, right in that if the key is more complicated, this method is not applicable. (You can however gain with the ST only if calculating the key is much slower than comparing them.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-20 12:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found