Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Re: Re: Is too little too much? Coding under the microscope...

by pope (Friar)
on Jun 28, 2001 at 10:13 UTC ( #92200=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Is too little too much? Coding under the microscope...
in thread Is too little too much? Coding under the microscope...

Err, yes the logic is correct. But why don't just write that as simple as this?
$newest = (sort { -M $a <=> -M $b } <*.log>)[0];
or if you have large amount of files, you'd better cache the result of -M:
$newest= (sort { ($m{$a} ||= -M $a) <=> ($m{$b} ||= -M $b) } <*.log>)[ +0];

Replies are listed 'Best First'.
Re: Is too little too much? Coding under the microscope...
by Abigail (Deacon) on Jun 28, 2001 at 20:57 UTC
    Well, if you have a large amount of files and you want to find the newest, the only reasonable optimization of your sort is the removal of said sort! You don't need more than a single pass (using linear time) to find an extreme.
    my $extreme = [365_000, undef]; foreach (<*.log>) { $extreme = [-M, $_] if $extreme -> [0] > -M } my $newest = $extreme -> [1];
    And if you want to sort them, faster than an Orcish Maneuvre or a Schwartian Transform is the Guttman-Rosler Transform:
    @files = map {substr $_ => 18} sort map {sprintf "%017.10f %s" => -M, $_} <*.log>;

    -- Abigail

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2020-02-25 04:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (108 votes). Check out past polls.

    Notices?