Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^7: Benchmark, -s versus schwartzian

by fizbin (Chaplain)
on Aug 25, 2004 at 20:09 UTC ( [id://385804]=note: print w/replies, xml ) Need Help??


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

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@/

Replies are listed 'Best First'.
Re^8: Benchmark, -s versus schwartzian (smarter [sort])
by tye (Sage) on Aug 25, 2004 at 20:32 UTC

    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@/

Log In?
Username:
Password:

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

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

    No recent polls found