C++ STL Map vs Vector speed

后端 未结 12 1049
日久生厌
日久生厌 2020-12-16 11:56

In the interpreter for my experimental programming language I have a symbol table. Each symbol consists of a name and a value (the value can be e.g.: of type string, int, fu

相关标签:
12条回答
  • 2020-12-16 12:27

    When most interpreters interpret code, they compile it into an intermediate language first. These intermediate languages often refer to variables by index or by pointer, instead of by name.

    For example, Python (the C implementation) changes local variables into references by index, but global variables and class variables get referenced by name using a hash table.

    I suggest looking at an introductory text on compilers.

    0 讨论(0)
  • 2020-12-16 12:33

    A map is a good thing to use for a symbol table. but operator[] for maps is not. In general, unless you are writing some trivial code, you should use the map's member functions insert() and find() instead of operator[]. The semantics of operator[] are somewhat complicated, and almost certainly don't do what you want if the symbol you are looking for is not in the map.

    As for the choice between map and unordered_map, the difference in performance is highly unlikely to be significant when implementing a simple interpretive language. If you use map, you are guaranteed it will be supported by all current Standard C++ implementations.

    0 讨论(0)
  • 2020-12-16 12:35

    std::map's operator[] takes O(log(n)) time. This means that it is quite efficient, but you still should avoid doing the lookups over and over again. Instead of storing an index, perhaps you can store a reference to the value, or an iterator to the container? This avoids having to do lookup entirely.

    0 讨论(0)
  • 2020-12-16 12:37

    If you're going to use a vector and go to the trouble of caching the most recent symbol look up result, you could do the same (cache the most recent look-up result) if your symbol table were implemented as a map (but there probably wouldn't be a whole lot of benefit to the cache in the case of using a map). With a map you'd have the additional advantage that any non-cached symbol look ups would be much more performant than searching in a vector (assuming that the vector isn't sorted - and keeping a vector sorted can be expensive if you have to do the sort more than once).

    Take Neil's advice; map is generally a good data structure for a symbol table, but you need to make sure you're using it correctly (and not adding symbols accidentally).

    0 讨论(0)
  • 2020-12-16 12:38

    a std::map (O(log(n))) or a hashtable ("amortized" O(1)) would be the first choice - use custom mechanisms if you determin it's a bottleneck. Generally, using a hash or tokenizing the input is the first optimization.

    Before you have profiled it, it's most important that you isolate lookup, so you can easily replace and profile it.


    std::map is likely a tad slower for a small number of elements (but then, it doesn't really matter).

    0 讨论(0)
  • 2020-12-16 12:38

    Map is O(log N), so not as fast as positional lookup in an array. But the exact results will depend on a lot of factors, and so the best approach is to interface with the container in a way that allows you to swap between implementation later on. That is, write a "lookup" function that can be efficiently implemented by any suitable container, to allow yourself to switch and compare speeds of different implementation.

    0 讨论(0)
提交回复
热议问题