Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: How to add more conditional statements in an efficient manner (updated x2)

by haukex (Archbishop)
on May 02, 2017 at 19:32 UTC ( [id://1189346]=note: print w/replies, xml ) Need Help??


in reply to How to add more conditional statements in an efficient manner

For string eq and ne, I like huck's hash table idea. For the generic problem of testing something against multiple other things with the same condition, see threads like Testing multiple variables against the same criteria (IF statement). Just one of several Ways To Do It:

use Perl6::Junction qw/all/; my $sPhoneCapabilityCode = "00000490"; my $s3905CapabilityCode ="00000290"; chomp( my $sUnpackedCDPcap = <STDIN> ); if ( $sUnpackedCDPcap ne all($sPhoneCapabilityCode,$s3905CapabilityCod +e) ) { print "Bingo!\n"; }

Note I haven't benchmarked it*, but my feeling is this would probably be slower than hash lookups. If it is speed you're after, you might try Benchmarking different approaches. My gut feeling is that raw if ($x ne $y && $x ne $z && $x ne ... ) { and a regex (see e.g. this) might be fastest, along with hash lookups, followed by solutions using modules. But then again, don't get lost in premature optimizations either - one advantage of the example above is that the code reads very nicely :-)

* Update: Interesting, hash lookup outperforms the others by a large margin (code is below):

Rate junct regex raw hash junct 104/s -- -79% -87% -95% regex 504/s 386% -- -38% -74% raw 806/s 678% 60% -- -58% hash 1932/s 1763% 284% 140% --
#!/usr/bin/env perl use warnings; use strict; use Benchmark qw/cmpthese/; my @testdata = map { sprintf "%08d", $_*97 } 0..9999; my @m = qw/ 00000490 00000290 00000100 00000123 00000970 /; use Perl6::Junction qw/all/; # prep for hash my %h = map {$_=>1} @m; # prep for regex my ($re) = map {qr/\A(?:$_)\z/} join '|', map {quotemeta} sort { length $b <=> length $a } @m; cmpthese(-1, { raw => sub { my $found; for my $x (@testdata) { if ($x ne $m[0] && $x ne $m[1] && $x ne $m[2] && $x ne $m[3] && $x ne $m[4]) { $found++ } } die "raw: $found" unless $found==9999; }, hash => sub { my $found; for my $x (@testdata) { if ( !exists $h{$x} ) { $found++ } } die "hash: $found" unless $found==9999; }, regex => sub { my $found; for my $x (@testdata) { if ( $x!~$re ) { $found++ } } die "regex: $found" unless $found==9999; }, junct => sub { my $found; for my $x (@testdata) { if ( $x ne all(@m) ) { $found++ } } die "junct: $found" unless $found==9999; }, } );

Update 2: Inspired by Marshall's post about ne vs. !=, if you add that test to the above benchmark, it turns out that hashes still outperform it by a factor of roughly 2. Of course, numeric comparisons may not apply in your case!

Replies are listed 'Best First'.
Re^2: How to add more conditional statements in an efficient manner (updated x2)
by adamZ88 (Beadle) on May 07, 2017 at 04:21 UTC

    This is a very helpful response! Thanx

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1189346]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found