Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Two new scripts, fixed thinko in existing script.

  • Loading branch information...
commit c6bf27341240e68ad9ddadddd79c3c28bae10a96 1 parent f8077e2
@colomon authored
View
2  bin/collatz-recursive-ternary-cached.pl
@@ -13,7 +13,7 @@
}
sub collatz-length(Int $n) is cached {
- return 1 if $n eq 1;
+ return 1 if $n == 1;
1 + ($n %% 2 ?? collatz-length($n div 2) !! collatz-length(3 * $n + 1));
}
View
28 bin/collatz-recursive-ternary-hand-cached.pl
@@ -0,0 +1,28 @@
+multi sub trait_mod:<is>(Routine $r, :$cached!) {
+ my %cache;
+ #wrap the routine in a block that..
+ $r.wrap(-> $arg {
+ # looks up the argument in the cache
+ %cache.exists($arg)
+ ?? %cache{$arg}
+ # ... and calls the original, if it
+ # is not found in the cache
+ !! (%cache{$arg} = callwith($arg))
+ }
+ );
+}
+
+sub collatz-length(Int $n) {
+ return 1 if $n == 1;
+ state %lengths;
+ return %lengths{$n} if %lengths.exists($n);
+ %lengths{$n} = 1 + ($n %% 2 ?? collatz-length($n div 2) !! collatz-length(3 * $n + 1));
+}
+
+sub MAIN(*@numbers) {
+ for @numbers -> $n {
+ say "$n: " ~ collatz-length($n.Int);
+ }
+}
+
+
View
31 bin/timing-table-generator.pl
@@ -0,0 +1,31 @@
+my @numbers = 1..200, 10000..10200;
+my $rakudo = "perl6";
+my $niecza = "mono ../niecza/run/Niecza.exe";
+
+sub MAIN(*@scripts) {
+ my %rakudo-results;
+ my %niecza-results;
+ for @scripts -> $script {
+ my $start = now;
+ qqx/$rakudo $script { @numbers }/;
+ my $end = now;
+ %rakudo-results{$script} = ($end - $start).round(1/100);
+
+ next if $script ~~ /cached/ && $script !~~ /"hand-cached"/;
+
+ $start = now;
+ qqx/$niecza $script { @numbers }/;
+ $end = now;
+ %niecza-results{$script} = ($end - $start).round(1/100);
+ }
+
+ say '<table border="1" align="center"> <tr><td>Script</td><td>Rakudo</td><td>Niecza</td></tr>';
+ for %rakudo-results.pairs.sort(*.value) -> (:key($script), :value($time)) {
+ say "<tr>";
+ say " <td> <a href=\"https://github.com/colomon/perl6-collatz/blob/master/$script\"> $script </a> </td>";
+ say " <td> $time </td>";
+ say " <td> { %niecza-results{$script} // "N/A" } </td>";
+ say "</tr>";
+ }
+ say "</table>";
+}
Please sign in to comment.
Something went wrong with that request. Please try again.