in reply to Re: How can I find the index of the biggest element in an array?
in thread How can I find the index of the biggest element in an array?
I was wondering about the efficiency of this solution so I compared it with GrandFather's solution:
# x.pl use Benchmark qw(timethese cmpthese); cmpthese( -2, { GrandFather => sub { my $idxMax = 0; $ARGV[$idxMax] > $ARGV[$_] or $idxMax = $_ for 1 .. $#ARGV +; $idxMax; }, reisinge => sub { [ sort { $ARGV[$b] <=> $ARGV[$a] } 0..$#ARGV ]->[0]; } } );
These are the results of the benchmarking:
$ perl x.pl $(seq 1 1000) Rate reisinge GrandFather reisinge 3742/s -- -3% GrandFather 3864/s 3% -- $ perl x.pl $(seq 1 10000) Rate reisinge GrandFather reisinge 377/s -- -5% GrandFather 395/s 5% -- $ perl x.pl $(seq 1 100000) Rate reisinge GrandFather reisinge 34.1/s -- -18% GrandFather 41.6/s 22% --
GrandFathers's solution seems to be more efficient, i.e. it's faster (to run) and scales better. But mine is shorter :-).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How can I find the index of the biggest element in an array?
by marioroy (Prior) on Mar 15, 2017 at 08:55 UTC | |
by reisinge (Hermit) on Mar 15, 2017 at 11:22 UTC |