Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: largest number inside array/hash

by eric256 (Parson)
on Apr 06, 2004 at 18:37 UTC ( [id://343069]=note: print w/replies, xml ) Need Help??


in reply to largest number inside array/hash

You could always just sort it and then take the highest one. I'm not sure how that compares speed wise with just scanning threw 1 by 1. Of course you could just keep a $counter and increment it when adding and not decrement it when removing. Then there is no need to find the highest because you remember it.

my $highest; for (@array) { # or keys %hash $highest = $_ if $_ > $highest; }

___________
Eric Hodges

Replies are listed 'Best First'.
Re: Re: largest number inside array/hash
by eric256 (Parson) on Apr 06, 2004 at 20:46 UTC

    I did some quick playing with the methods shown here just to get an idea of speed. Before now i've never looked at List::Util because I could do it all myself but now I think I see the light. It provides an astounding speed bonus.

    use strict; use warnings; use Benchmark ; use List::Util qw(max shuffle); my @a = (1..500_000); my @b = shuffle (1..500_000); my $greatest; timethese(25, { 'sort_shuffled' => sub {($greatest)=sort{$b<=>$a}@b;}, 'grep_shuffled' => sub { grep($greatest=($_>$greatest)?$_:$greatest,@b); }, 'for_shuffled' => sub { $greatest = 0; for (@b) { $greatest = $_ if $_ > $greatest; } }, 'max_shuffled' => sub { max(@b) }, 'sort_inorder' => sub {($greatest)=sort{$b<=>$a}@a;}, 'grep_inorder' => sub { grep($greatest=($_>$greatest)?$_:$greatest,@a); }, 'for_inorder' => sub { $greatest = 0; for (@a) { $greatest = $_ if $_ > $greatest; } }, 'max_inorder' => sub { max(@a) } }); __DATA__ Benchmark: timing 25 iterations of for_inorder, for_shuffled, grep_ino +rder, grep_shuffled, max_inord er, max_shuffled, sort_inorder, sort_shuffled... for_inorder: 5 wallclock secs ( 5.17 usr + 0.00 sys = 5.17 CPU) @ +4.83/s (n=25) for_shuffled: 4 wallclock secs ( 3.55 usr + 0.00 sys = 3.55 CPU) @ + 7.05/s (n=25) grep_inorder: 4 wallclock secs ( 3.75 usr + 0.00 sys = 3.75 CPU) @ + 6.67/s (n=25) grep_shuffled: 4 wallclock secs ( 3.70 usr + 0.00 sys = 3.70 CPU) @ + 6.75/s (n=25) max_inorder: 1 wallclock secs ( 0.89 usr + 0.00 sys = 0.89 CPU) @ 2 +8.09/s (n=25) max_shuffled: 1 wallclock secs ( 0.91 usr + 0.00 sys = 0.91 CPU) @ +27.59/s (n=25) sort_inorder: 1 wallclock secs ( 1.73 usr + 0.02 sys = 1.75 CPU) @ +14.28/s (n=25) sort_shuffled: 36 wallclock secs (34.34 usr + 0.05 sys = 34.39 CPU) @ + 0.73/s (n=25)

    I was curious if anyone could explain why grep is so much faster thant the for? Ohh and how do people do those cool comparison benchmarks with the chart?


    ___________
    Eric Hodges
      Ohh and how do people do those cool comparison benchmarks with the chart?
      Use "cmpthese" instead of "timethese".

      MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
      I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
      ** The third rule of perl club is a statement of fact: pod is sexy.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-20 03:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found