Created
March 12, 2017 05:45
-
-
Save LLFourn/8c3e895e789fab957355ce23c9420133 to your computer and use it in GitHub Desktop.
Shows the difference in performance between native ints and vanilla.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl6 | |
sub perl6-loop($n) | |
{ | |
my $sum = 0; | |
for 1..$n -> $i { | |
for 1..$i -> $j { | |
$sum += $i+$j; | |
} | |
} | |
} | |
sub c-loop($n) | |
{ | |
my $sum = 0; | |
loop (my $i = 1; $i <= $n; $i++) { | |
loop (my $j = 1; $j <= $i; $j++) { | |
$sum += $i+$j; | |
} | |
} | |
} | |
sub native-loop(int $n) { | |
my int $sum = 0; | |
loop (my int $i = 1; $i <= $n; $i++) { | |
loop (my int $j = 1; $j <= $i; $j++) { | |
$sum = $sum + $i + $j; | |
} | |
} | |
} | |
sub MAIN($n = 10⁴) | |
{ | |
my $last; | |
for &perl6-loop,&c-loop,&native-loop { | |
my $start = now; | |
.($n); | |
my $t = now - $start; | |
say "{.name}: $t { "({($last/$t).round(.01)} times faster)"if $last}"; | |
$last = $t; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment