http://qs321.pair.com?node_id=484170


in reply to Re: How to list files in dir with respect to time ?
in thread How to list files in dir with respect to time ?

You could use the Orcish Maneuver to make your code go faster...

# Original code for ( sort { -M $a <=> -M $b } glob ... ) # Orcish Maneuver my %t; for ( sort { ($t{$a} ||= -M $a) <=> ($t{$b} ||= -M $b) } glob ... )


Replies are listed 'Best First'.
Re^3: How to list files in dir with respect to time ?
by anonymized user 468275 (Curate) on Aug 17, 2005 at 08:36 UTC
    I've looked into this now - I was expecting sort to be smart enough to do that manouvre itself. For those who don't understand what it's doing, it is forcing the sort routine to perform the -M operation only once per file, but at the overhead of a hash lookup per sort iteration (there are geometrically upon n sort iterations per n files). sort + block could be optimised for minimal calculation of each side of the <=> operator (or cmp as appropriate) -- after all it ought to know it's going to be doing that irrespective of what's in the block, so it's a safe optimisation for sort to be doing already. But the orcish manouevre appears to be necessary because no such optimisation has been implemented. Perhaps Sort::Key has the manoeuvre already, however.

    One world, one people

      Perhaps Sort::Key has the manoeuvre already, however

      well, it's not exactly the Orcish manouevre, but Sort::Key calculates every key only once.