Skip to content

Instantly share code, notes, and snippets.

@FROGGS

FROGGS/any.diff Secret

Created May 17, 2013 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FROGGS/d830a84c4bb9f6b2fe07 to your computer and use it in GitHub Desktop.
Save FROGGS/d830a84c4bb9f6b2fe07 to your computer and use it in GitHub Desktop.
optimize sort($, $)
diff --git a/src/core/Any.pm b/src/core/Any.pm
index d22ccfb..3102c1f 100644
--- a/src/core/Any.pm
+++ b/src/core/Any.pm
@@ -507,6 +507,12 @@ proto uniq(|) { * }
multi uniq(*@values) { @values.uniq }
proto sub sort(|) {*}
+multi sub sort(Callable $a, $b, $c) {
+ $a($b, $c)
+}
+multi sub sort(Mu \a, Mu \b) {
+ a cmp b
+}
multi sub sort(*@values) {
@values.at_pos(0).^does(Callable)
?? do { my $cmp := @values.shift; @values.sort($cmp) }
$ time perl6 -e 'sort(2, 1) for ^100000' # 100.000
real 0m8.487s
user 0m8.321s
sys 0m0.144s
$ time perl6 -e 'sort(2, 1) for ^10000' # 10.000
real 0m16.707s
user 0m16.525s
sys 0m0.152s
$ time perl6 -e 'sort(2, 1) for ^100000' # 100.000
real 2m47.941s
user 2m47.462s
sys 0m0.288s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment