haukex the test you are proposing removes only 1 item from @numbers. If the test removed more than one numbers (or all) one after the other (after a shuffle) could be more realistic (and my hash-based approach could get a chance too :)):

use warnings; use strict; use Benchmark qw/cmpthese/; use List::Util qw/shuffle/; my @numbers = ( 0..20 ); my @indices = shuffle 0..$#numbers; my @expect = ( 0..2,4..20 ); use constant TEST => 0; cmpthese(-2, { splice => sub { my @output = @numbers; for my $index (@indices){ splice @output, 0, 1; } join("\0", @output) eq join("\0", @expect) or die if TEST; }, grep => sub { # https://www.perlmonks.org/?node_id=11123877 my @output; for my $index (@indices){ @output = @numbers[ grep $_ != $index, 0 .. $#numbers ]; } join("\0", @output) eq join("\0", @expect) or die if TEST; }, hash => sub { my %ha; @ha{0..$#numbers} = @numbers; for my $index (@indices){ delete $ha{$index}; } my @output = map { $ha{$_} } sort keys %ha; join("\0", @output) eq join("\0", @expect) or die if TEST; }, }); __END__ Rate grep hash splice grep 30916/s -- -85% -96% hash 205775/s 566% -- -71% splice 707918/s 2190% 244% --

With the above, grep is disadvantaged and splice it just a guess by me (it always removes the first item).

The bottom line: in a loop of removals, hash-based approach can be a contestant too although splice is by far the best but it messes the indices.

bw, bliako


In reply to Re: Fastest way to "pick without replacement" by bliako
in thread Fastest way to "pick without replacement" by haukex

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.