Why not post it in the thread? Here's an adapted bench with a version of my code modified slightly to be callable like the others:
It is interesting to see that my code wins hands down when N/MAX is close to 1. Even when the ratio of N/MAX shrinks, my code loses a lot of ground but keeps beating Limbic's proposition. None of this matters much though since for large MAX, all of the solutions perform very similarly, even if the trends remain clear.
So out of curiosity I added the following bit to the code:
baseline => sub {
my ( $n, $list ) = @_;
return ( sort { $a <=> $b } @$list )[ @$list - $n .. $#$list ]
+;
},
Well, I'll just say let's pack the bags and go home folks. Nothing to see here, move along. As I said: clever Perl code vs builtin: clever Perl code loses. Grossly disproportionately, in fact.
(Spoiler for anyone who doesn't care to run the benchmarks: in all cases the baseline sort version runs hundreds to thousands of times faster than the other solutions.)
Makeshifts last the longest. |