diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-04-21 13:00:56 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-04-21 13:00:56 +0100 |
| commit | 0691e05a6750e20c750d46bb4488a574bb147d8a (patch) | |
| tree | ce9bf4cb513d2e94b195cf3154c1709f4f591387 /challenge-109/james-smith | |
| parent | 39c3ac8c65f6fa3d0a86e5af47727eec086e162a (diff) | |
| download | perlweeklychallenge-club-0691e05a6750e20c750d46bb4488a574bb147d8a.tar.gz perlweeklychallenge-club-0691e05a6750e20c750d46bb4488a574bb147d8a.tar.bz2 perlweeklychallenge-club-0691e05a6750e20c750d46bb4488a574bb147d8a.zip | |
change the n-1 to n/2 or (rather n>>1) ~ 50% faster - approx 80K calcs per second for the for loop version
Diffstat (limited to 'challenge-109/james-smith')
| -rw-r--r-- | challenge-109/james-smith/perl/ch-1.pl | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/challenge-109/james-smith/perl/ch-1.pl b/challenge-109/james-smith/perl/ch-1.pl index 1b89226a6a..c7820fb3d2 100644 --- a/challenge-109/james-smith/perl/ch-1.pl +++ b/challenge-109/james-smith/perl/ch-1.pl @@ -19,7 +19,7 @@ is( chowla_for($_), $answer[ $_ ] ) foreach 1..20; done_testing(); ## We will quickly run benchmarking... -## This suggests the for loop to be approximately 40-50% +## This suggests the for loop to be approximately 40% ## faster than the map solution... ## It is also 9 characters shorter... @@ -30,15 +30,15 @@ cmpthese(1_000_000, { ## ## Rate Map For -## Map 38670/s -- -33% -## For 57670/s 49% -- +## Map 59524/s -- -26% +## For 79936/s 34% -- ## sub chowla_map { my ($t,$n) = (0,@_); ## First attempt - the one-liner is to write this as a map, ## we add $t at the end which is the value returned - ( map { (($n%$_) || ($t+=$_)) && () } 2..$n-1 ), $t; + ( map { (($n%$_) || ($t+=$_)) && () } 2..$n>>1 ), $t; } sub chowla_for { @@ -56,7 +56,7 @@ sub chowla_for { ## ($condition)||($fun()) ## * in perl `foreach` and `for` are synonymous - so we can shorten - ($n%$_)||($t+=$_) for 2..$n-1; + ($n%$_)||($t+=$_) for 2..$n>>1; ## Now a quick "shortening" - if there is no specific return ## statement - we can just omit the return in the last statement... |
