Should I use if statements or a hashmap (std::map / std::unsorted_map)?

I am making a scripting language in C++ and I want to call functions when I hit keywords, so I want to know before I make big changes, should I use if statements or std::map / std::unsorted map or something else for deciding on what and where to call?

I know that std::map and std::unsorted_map come with a performance penalty and it might matter as the language is interpreted, but it's more convenient and clean, so which one should I use?
Which one allows you to make the most forward progress the quickest (and most reliably)?

If you make a half-decent job of encapsulating the "resolve keyword" functionality, then changing from one to the other is an opaque change to the rest of your software.

> I know that std::map and std::unsorted_map come with a performance penalty
Premature optimisation disease.
And you're probably wrong anyway.
map lookups are O(log2(N)) whereas your if/else chain is O(N).
Above some N, maps are going to start winning.

The huge challenge is creating something that actually works as you want.

When (and only when) your program is functionally complete (and you have a thorough set of tests) can you start to evaluate real-world performance.
@selem_c Thanks! I will use std::map then :), but I'm pretty new to C++ and people told me that std::map was slow so haha, anyway thanks for the answer :)
IDK who you are talking to, but the whole point of a hash map is O(1) lookup...
std::map isn't hash - its usually red-black tree. std::unordered_map is hash.
Oops, I almost always use an unordered_map.
std::map still has O(log n) lookup, which is hardly a performance penalty.
One idea I had is to look at how actual open-source projects implement this sort of thing. For example, you could look through the CPython implementation of Python's interpreter and see how it looks up variables in global and local scopes. But trudging through a large codebase can be a daunting task.
Topic archived. No new replies allowed.