Last active
September 13, 2018 09:07
-
-
Save MattOates/83255d115ae8f98e65a161192c99fab4 to your computer and use it in GitHub Desktop.
Some additional benched P6 examples from Tyil's Hackerrank post https://www.tyil.nl/post/2018/09/13/hackerrank-solutions-python3-and-perl6-part-1/
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
use Stats; | |
sub bench($name, &code) { | |
my ($start,$end); | |
my @times; | |
for 1..100 { | |
$start = now; | |
code(); | |
$end = now; | |
@times.push($end-$start); | |
} | |
my $sd = sd @times; | |
my $mu = mean @times; | |
$*ERR.say("$name: ran in $mu seconds (σ = $sd seconds)"); | |
} | |
#matt_staircaise: ran in 0.001937888198757764 seconds (σ = 0.0021329978923143897 seconds) | |
#tyil_staircase: ran in 0.042826549673016044 seconds (σ = 0.004286578353740222 seconds) | |
bench 'matt_staircaise', { | |
for 1..(100+1) -> $i { | |
print(" " x 1 + 100 - $i); | |
print("#" x $i - 1); | |
print("\n"); | |
} | |
} | |
bench 'tyil_staircase', { | |
for 1..(100+1) -> $i { | |
print(" ") for 0..(100 - $i); | |
print("#") for ^$i; | |
print("\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment