Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Re: largest number inside array/hash

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


in reply to Re: largest number inside array/hash
in thread largest number inside array/hash

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

Replies are listed 'Best First'.
Re: Re: Re: largest number inside array/hash
by PodMaster (Abbot) on Apr 06, 2004 at 21:47 UTC
    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://343110]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 23:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found