Skip to content

Commit

Permalink
First stab at timing harness, two example sequences.
Browse files Browse the repository at this point in the history
  • Loading branch information
colomon committed Dec 17, 2012
1 parent d61d83c commit 12732f2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
15 changes: 15 additions & 0 deletions 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);
}
}


11 changes: 11 additions & 0 deletions 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);
}
}


15 changes: 15 additions & 0 deletions 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.