Mine is also way slower than I expected, here just to be complete :)
cmpthese (-2, {
sortfirst => sub {
my @list = -5..5;
@list = sort { $a <=> $b } @list;
@list = ((grep { $_ >= 0 } @list), (grep { $_ < 0 } @list));
DO_CHECK and (Compare (\@list, [0..5, -5..-1]) or die "@list")
+;
},
grepfirst => sub {
my @list = -5..5;
my @pos = grep { $_ >= 0 } @list;
my @neg = grep { $_ < 0 } @list;
@list = ((sort { $a <=> $b } @pos), (sort { $a <=> $b } @neg))
+;
DO_CHECK and (Compare (\@list, [0..5, -5..-1]) or die "@list")
+;
},
packunpck => sub {
my @list = map { unpack "l>", $_ } sort map { pack "l>", $_ }
+-5..5;
DO_CHECK and (Compare (\@list, [0..5, -5..-1]) or die "@list")
+;
},
pryrt => sub {
my @list = -5..5;
sub sgn { $_[0] < 0 ? -1 : 1 }
@list = sort { (sgn ($b) <=> sgn ($a)) || ($a <=> $b) } @list;
DO_CHECK and (Compare (\@list, [0..5, -5..-1]) or die "@list")
+;
},
choroba => sub {
my @list = -5..5;
@list = sort {
((-1, 0, 1)[$a <=> 0] <=> (-1, 0, 1)[$b <=> 0]) || ($a <=>
+ $b)
} @list;
DO_CHECK and (Compare (\@list, [0..5, -5..-1]) or die "@list")
+;
},
});
Rate pryrt packunpck choroba grepfirst sortfirst
pryrt 123672/s -- -35% -39% -66% -73%
packunpck 190037/s 54% -- -7% -48% -59%
choroba 204244/s 65% 7% -- -44% -56%
grepfirst 367991/s 198% 94% 80% -- -20%
sortfirst 460545/s 272% 142% 125% 25% --
Enjoy, Have FUN! H.Merijn
In reply to Re: Fastest way to sort a list of integers into 0,1,2,3,-3,-2,-1
by Tux
in thread Fastest way to sort a list of integers into 0,1,2,3,-3,-2,-1
by haukex
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.