Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Pair of items

by tadman (Prior)
on Jul 29, 2003 at 20:21 UTC ( #278969=note: print w/ replies, xml ) Need Help??

in reply to Pair of items

If you're only using three at a time, you could always do this:

sub is_ok { $_[0][0] != $_[1][0] && $_[0][0] != $_[1][1] && $_[0][0] != $_[1][2] && $_[0][1] != $_[1][0] && $_[0][1] != $_[1][1] && $_[0][1] != $_[1][2] && $_[0][2] != $_[1][0] && $_[0][2] != $_[1][1] && $_[0][2] != $_[1][2]; }
Very rudimentary, but at the same time, this "unrolled" version is way faster than any list or hash based operation. Using a quick Benchmark test, it was 80% faster than other bits posted here.

Comment on Re: Pair of items
Download Code
Replies are listed 'Best First'.
Re2: Pair of items
by dragonchild (Archbishop) on Jul 29, 2003 at 20:26 UTC
    While faster, it is more prone to error should there be any change in requirements. It also needs a helluvalot of commenting so that your maintenance programmer(s) (which could very well be you!) don't make a well-meaning change and screw it up.

    Like all optimizations, it should be done only if that section of code has been identified as a bottleneck through profiling. (Never use analysis as the only source of information when profiling. Using a profiler is the only way to truly know where your bottlenecks are.)

    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      I agree that it's awfully simplistic, but there are occasions, like bioinformatics, which might use a routine like this in some capacity. I've just given two different versions for comparision and discussion/argument and whatnot.

      I'd go for the hash based approach.

      However, even a function that's intentionally crippled like that can be made "safe" by naming it something like compare_triplets or what have you. You'd have to be pretty daring to feed a function like that anything but what's expected, though of course, even this precaution may not protect you from the inevitable.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2015-11-28 09:29 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (740 votes), past polls