Skip to content
Browse files

First stab at timing harness, two example sequences.

  • Loading branch information...
1 parent d61d83c commit 12732f2f314fddab4335cc02b2bd3c72d41d5d97 @colomon committed
Showing with 41 additions and 0 deletions.
  1. +15 −0 bin/collatz-recursive.pl
  2. +11 −0 bin/collatz-sequence.pl
  3. +15 −0 bin/timing-harness.pl
View
15 bin/collatz-recursive.pl
@@ -0,0 +1,15 @@
+sub collatz-length(Int $n) {
+ given $n {
+ when 1 { 1 }
+ when * %% 2 { collatz-length($_ / 2) }
+ default { collatz-length(3 * $_ + 1) }
+ }
+}
+
+sub MAIN(*@numbers) {
+ for @numbers -> $n {
+ say "$n: " ~ collatz-length($n.Int);
+ }
+}
+
+
View
11 bin/collatz-sequence.pl
@@ -0,0 +1,11 @@
+sub collatz-length(Int $start) {
+ +($start, { when * %% 2 { $_ / 2 }; when * !%% 2 { 3 * $_ + 1 }; } ... 1);
+}
+
+sub MAIN(*@numbers) {
+ for @numbers -> $n {
+ say "$n: " ~ collatz-length($n.Int);
+ }
+}
+
+
View
15 bin/timing-harness.pl
@@ -0,0 +1,15 @@
+my $perl6 = @*ARGS.shift;
+my @numbers = 1..100, 10000..10100;
+
+my %results;
+for @*ARGS -> $script {
+ my $start = now;
+ qqx/$perl6 $script { @numbers }/;
+ my $end = now;
+
+ %results{$script} = $end - $start;
+}
+
+for %results.pairs.sort(*.value) -> (:key($script), :value($time)) {
+ say "$script: $time seconds";
+}

0 comments on commit 12732f2

Please sign in to comment.
Something went wrong with that request. Please try again.