Skip to content

Instantly share code, notes, and snippets.

@hinrik
Created February 10, 2015 16:47
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 hinrik/7cf05941c6b32d667c39 to your computer and use it in GitHub Desktop.
Save hinrik/7cf05941c6b32d667c39 to your computer and use it in GitHub Desktop.
# <0.1 sec (Perl5)
#sub fast {
# my @products;
# for (my $i = 1; $i <= 999; $i++) {
# for (my $n = $i; $n <= 999; $n++) {
# push @products, $i * $n;
# }
# }
# return \@products;
#}
# 1.6 sec
sub slow {
my @products;
loop (my $i = 100; $i <= 999; $i++) {
loop (my $n = $i; $n <= 999; $n++) {
@products.push: $i * $n;
}
}
return @products;
}
# 1.7 sec
sub slower {
my @products;
for 100..999 -> $i {
for $i..999 -> $n {
@products.push: $i * $n;
}
}
return @products;
}
# 3.7 sec
sub slowest {
return gather for 100..999 -> $i {
take $i * $_ for $i..999;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment