aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-06-24 07:33:35 +0100
committerdrbaggy <js5@sanger.ac.uk>2021-06-24 07:33:35 +0100
commite275b63c8570d266ac6e78dee8edbbad9fc7fcb7 (patch)
treea7e6e7a0ce67daf95b3e664ebb637e28caac6f28
parent5e569cc0502be055b953b9cca077fd00bfaefe1c (diff)
downloadperlweeklychallenge-club-e275b63c8570d266ac6e78dee8edbbad9fc7fcb7.tar.gz
perlweeklychallenge-club-e275b63c8570d266ac6e78dee8edbbad9fc7fcb7.tar.bz2
perlweeklychallenge-club-e275b63c8570d266ac6e78dee8edbbad9fc7fcb7.zip
speed up by using $a rather than a local variable
-rw-r--r--challenge-118/james-smith/perl/ch-1.pl7
1 files changed, 3 insertions, 4 deletions
diff --git a/challenge-118/james-smith/perl/ch-1.pl b/challenge-118/james-smith/perl/ch-1.pl
index 6dadf2038d..df84ea81f1 100644
--- a/challenge-118/james-smith/perl/ch-1.pl
+++ b/challenge-118/james-smith/perl/ch-1.pl
@@ -32,7 +32,7 @@ my @TESTS = (
is( is_binary_palindrome($_->[0]), $_->[1] ) foreach @TESTS;
is( is_binary_palindrome_string($_->[0]), $_->[1] ) foreach @TESTS;
-cmpthese( 250_000, {
+cmpthese( 100_000, {
'array' => sub { is_binary_palindrome($_->[0]) foreach @TESTS },
'string' => sub { is_binary_palindrome_string($_->[0]) foreach @TESTS },
} );
@@ -41,10 +41,9 @@ done_testing();
sub is_binary_palindrome_string {
## This is the core perl solution convert to binary using sprintf
- ## [this is faster than unpack!]
+ ## [this is faster than unpack and doesn't have issue with leading 0s!]
## and compare with reverse...
- my $t = sprintf '%b', shift;
- return ($t eq reverse $t) || 0;
+ return ( ( $a = sprintf '%b', $_[0] ) eq reverse $a ) || 0;
}
sub is_binary_palindrome {