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


in reply to Which one is best for optimization

Solution 1 requires only an iterator to keep track of where you are in the hash. You will visit each entry once, and once only.

Solution 2 requires sorting the list, so at a minimum you are going to visit each entry anyway, and the sort algorithm that takes O(n) time in the general case has yet to be invented. This will take more time, no matter what, and worse, it may potentially all for naught if there are no files that are larger than your limit.

The only sane approach is solution 1.

• another intruder with the mooring in the heart of the Perl

Replies are listed 'Best First'.
Re^2: Which one is best for optimization
by salva (Canon) on Sep 29, 2008 at 09:50 UTC
    and the sort algorithm that takes O(n) time in the general case has yet to be invented
    Well, but for this particular case, sorting numbers, it has already been invented!, it is called radix sort and there are at least two Perl implementations available from CPAN: Sort::Radix (pure perl) and Sort::Key::Radix (XS).

      The whole point is that you don't need to sort! It's a net loss whichever way you cut it.

      • another intruder with the mooring in the heart of the Perl