in reply to Fastest way to sort a list of integers into 0,1,2,3,-3,-2,-1
use warnings; use strict; use Benchmark 'cmpthese'; use constant DO_CHECK => 0; use if DO_CHECK, 'Data::Compare', qw/Compare/; my @input = (-50..50); my @output = (0..50,-50..-1); use List::Util 'shuffle'; srand 123; @input = shuffle @input; # NB. NB. NB. NB. cmpthese(-2, { packz => sub { my @list = @input; @list = unpack 'i*', pack 'I*', sort { $a <=> $b } unpack 'I*', pack 'i*', @list; Compare(\@list,\@output) or die "@list" if DO_CHECK; }, Eily => sub { # https://www.perlmonks.org/?node_id=1229411 my @list = @input; @list = sort { ~$b <=> ~$a } @list; Compare(\@list,\@output) or die "@list" if DO_CHECK; }, }); __END__ Rate Eily packz Eily 6495/s -- -66% packz 18967/s 192% --
Slow machine here. Shuffled input is important, otherwise Eily's code seems to be faster.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Fastest way to sort a list of integers into 0,1,2,3,-3,-2,-1
by Eily (Monsignor) on Feb 05, 2019 at 17:12 UTC | |
|
Re^2: Fastest way to sort a list of integers into 0,1,2,3,-3,-2,-1
by haukex (Archbishop) on Feb 05, 2019 at 16:20 UTC | |
|
Re^2: Fastest way to sort a list of integers into 0,1,2,3,-3,-2,-1
by vr (Curate) on Feb 05, 2019 at 18:25 UTC | |
|
Re^2: Fastest way to sort a list of integers into 0,1,2,3,-3,-2,-1
by ikegami (Patriarch) on Feb 06, 2019 at 03:31 UTC | |
by vr (Curate) on Feb 06, 2019 at 09:33 UTC | |
by ikegami (Patriarch) on Feb 06, 2019 at 20:10 UTC |