fake! you dont need to sort the array to times... because u know where the
lowest hand the higest is.
Less of the half of 11 wallclocks would be what i expect.

> -----Ursprüngliche Nachricht-----
> Von: Sudarshan Raghavan [mailto:[EMAIL PROTECTED]]
> Gesendet am: Samstag, 31. August 2002 04:11
> An: Perl beginners
> Betreff: Re: find the lowest number?
>
> On 30 Aug 2002, Felix Geerinckx wrote:
>
> > There is no need to sort the full array (as others suggested) to just
> > find the lowest and highest number when an O(n) operation will
> > suffice:
>
> Precisely the comment that I had a few months back for a similar question.
> Read through this thread
> http://archive.develooper.com/beginners%40perl.org/msg22716.html
>
> >
> >     my ($min, $max);
> >     $min = $max = $array[0];
> >     for (1..@array-1) {
> >         $min = $array[$_] if $array[$_] < $min;
> >         $max = $array[$_] if $array[$_] > $max;
> >     }
> >     print "min = $min, max = $max\n";
>
> For further proof here is a benchmarking result
> #!/usr/local/bin/perl -w
> use strict;
> use Benchmark;
>
> my @arr = qw(34 12 23 45 11 91 32);
> sub using_sort {
>         my $min = (sort {$a <=> $b} @arr)[0];
>         my $max = (reverse sort {$a <=> $b} @arr)[0];
> }
>
> sub using_for {
>         my ($min, $max);
>         $min = $max = $arr[0];
>         for (1..@arr-1) {
>               $min = $arr[$_] if $arr[$_] < $min;
>               $max = $arr[$_] if $arr[$_] > $max;
>       }
> }
>
> timethese (1000000, { using_sort => \&using_sort,
> using_for => \&using_for });
>
> =cut
> Benchmark: timing 1000000 iterations of using_for, using_sort...
>  using_for: 11 wallclock secs (11.60 usr +  0.04 sys = 11.64 CPU) @
> 85910.65/s (n=1000000)
> using_sort:  8 wallclock secs ( 7.63 usr +  0.06 sys =  7.69 CPU) @
> 130039.01/s (n=1000000)
>
> Note that even with two sorts and a reverse the using_sort subroutine
> is faster in terms of speed.
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to