Front page | perl.beginners | Postings from February 2002

## RE: finding max value

From:
Chas Owens
Date:
February 13, 2002 11:52
Subject:
RE: finding max value
Message ID:
1013629892.5497.9.camel@tert.icallinc.com
```On Wed, 2002-02-13 at 13:50, Jeremy Vinding wrote:
> > >there must be a flaw in my test here:
> > >
> > >my @bob = rand for (1..20);
> > >my @joe = rand for (1..10_000);
> >
> > Those.
> >
> >   my @bob = map rand, 1 .. 20;
> >   my @joe = map rand, 1 .. 10_000;
> >
>
> duh... thx
> of course, the results still favor sort:
>
> Benchmark: timing 1000000 iterations of for 10_000 elems, for 20 elems, sort
> 10_000 elems, sort 20 elems...
> for 10_000 elems:  7 wallclock secs ( 5.11 usr +  0.02 sys =  5.13 CPU) @
> 194931.77/s (n=1000000)
> for 20 elems:  8 wallclock secs ( 5.44 usr +  0.01 sys =  5.45 CPU) @
> 183486.24/s (n=1000000)
> sort 10_000 elems:  4 wallclock secs ( 2.99 usr + -0.02 sys =  2.97 CPU) @
> 336700.34/s (n=1000000)
> sort 20 elems:  3 wallclock secs ( 3.22 usr + -0.04 sys =  3.18 CPU) @
> 314465.41/s (n=1000000)
>

#sort ascending and get the last element
\$max = (sort { \$a <=> \$b } @input)[-1];

method or Schwartz's more efficient

#sort descending and assign to a list of one variable
(\$max) = sort { \$b <=> \$a } @input;

method?

--
Today is Prickle-Prickle the 44th day of Chaos in the YOLD 3168
Wibble.