Skip to content

Instantly share code, notes, and snippets.

@ab5tract
Last active August 29, 2015 14:26
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 ab5tract/1c394b6ef32bbf27374c to your computer and use it in GitHub Desktop.
Save ab5tract/1c394b6ef32bbf27374c to your computer and use it in GitHub Desktop.
for-vs-while-int-vs-Int
my $begin = DateTime.now;
say "Beginning at $begin";
my int @range = 0..^400;
my $count = 0;
for @range -> int $i {
for @range -> int $j {
for @range -> int $k {
$count = $count + 1;
}
}
}
say "done $count in {now - $begin.Instant} seconds";
# doing time inside the script, so that we don't measure startup time
my $begin = now;
say "Starting at $begin";
my int $count = 0;
my int $i = 0;
while $i < 400 {
my int $j = 0;
while $j < 400 {
my int $k = 0;
while $k < 400 {
$k = $k + 1;
$count = $count + 1;
}
$j = $j + 1;
}
$i = $i + 1;
}
my $interval = now - $begin;
print "done $count in $interval seconds\n";
done 64000000 in 4.035 seconds
perl benchmark.pl
done 64000000
10.51user 0.00system 0:10.51elapsed 100%CPU (0avgtext+0avgdata 7536maxresident)k
0inputs+0outputs (0major+527minor)pagefaults 0swaps
my $count;
my $i = 0;
while ( $i < 400 ) {
my $j = 0;
while ( $j < 400 ) {
my $k = 0;
while ( $k < 400 ) {
$k = $k + 1;
$count = $count + 1;
}
$j = $j + 1;
}
$i = $i + 1;
}
print "done $count\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment