Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.