Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: comparison with bitwise operators

by LanX (Saint)
on Jan 14, 2011 at 02:06 UTC ( [id://882274]=note: print w/replies, xml ) Need Help??


in reply to Re: comparison with bitwise operators
in thread comparison with bitwise operators

When checking against just two values you are certainly right, but for more values the other approaches scale better.

Cheers Rolf

Replies are listed 'Best First'.
Re^3: comparison with bitwise operators
by Marshall (Canon) on Jan 14, 2011 at 03:32 UTC
    I think we are in agreement.
    I was keying on the OP's statement of the problem: "compare a value to two numbers".
    The proposed formulation is very, very good! I see no reason to "improve" it.

    For efficiency, with a much larger set of possibilities, "does value X exist within a data set?", I would think that the List::MoreUtils qw (any); solution is the best for two reasons:
    a)Since it is an XS module it will run faster than native Perl code,
    b)the grep solution will examine all possibilities in order to return the scalar result of number of "matches" whereas a well written XS module would return true immediately upon the first match.

    The Perl "or" code will also "give up" and stop processing immediately upon the first successful match (OR condition is satisfied). I think this is just fine for say 1/2 dozen "or" conditions. Of course the expected probability of returning "true" should be taken into account in the ordering of an "if" statement. This may result in faster code than using say the "smart match" operator in Perl 5.10. Benchmark the program if there is any doubt.

      Agreed, just that I'd prefer the hash solution in cases where scalability matters.

      (Furthermore List::MoreUtils isn't core.)

      Cheers Rolf

      UPDATE: 3 downvotes???

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-03-29 06:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found