Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Hash versus chain of elsifs

by kikuchiyo (Friar)
on Nov 22, 2021 at 09:28 UTC ( #11139006=note: print w/replies, xml ) Need Help??

in reply to Hash versus chain of elsifs

Is the set of strings fixed and known in advance?

If yes, look up minimal perfect hashing. One has to be generated specifically for your set of strings, and the generation part is usually expensive, but once it's done it can tell whether an input string is in the set with just one hash lookup and one string comparison.

Compilers and similar programs use this technique when they want to match tokens from the input stream against a small, known set of keywords.

I don't know if it's worth the effort, though, if you're working in Perl - just use a regular hash and be done with it. You may want to look at the problem again when you're optimizing your program, but you'll likely find that hash access is not your main bottleneck.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2022-08-08 00:55 GMT
Find Nodes?
    Voting Booth?

    No recent polls found