Front page | perl.beginners | Postings from February 2002

## RE: finding max value

From:
Jeremy Vinding
Date:
February 13, 2002 12:07
Subject:
RE: finding max value
Message ID:
GNEBJNPEEJMMLILBFEBHGEBPDAAA.jvinding@aol.com
```> > 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?
>

more like the first:

my \$max = (sort { \$b <=> \$a } @input)[0];

changing it to the more efficient version yields these results:

sort 10_000 elems:  4 wallclock secs ( 2.94 usr +  0.00 sys =  2.94 CPU) @
340136.05/s (n=1000000)
sort 20 elems:  4 wallclock secs ( 3.15 usr +  0.00 sys =  3.15 CPU) @
317460.32/s (n=1000000)

no difference really.

```