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

Re: Which one is best for optimization

by grinder (Bishop)
on Sep 29, 2008 at 09:42 UTC ( #714289=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2022-05-21 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (76 votes). Check out past polls.

    Notices?