Skip to content

Instantly share code, notes, and snippets.

@LLFourn
Created March 12, 2017 05:45
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 LLFourn/8c3e895e789fab957355ce23c9420133 to your computer and use it in GitHub Desktop.
Save LLFourn/8c3e895e789fab957355ce23c9420133 to your computer and use it in GitHub Desktop.
Shows the difference in performance between native ints and vanilla.
#!/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