What is more efficient a switch case or an std::map

前端 未结 6 1077
星月不相逢
星月不相逢 2020-12-28 13:57

I\'m thinking about the tokenizer here.
Each token calls a different function inside the parser.
What is more efficient:

  • A map of std::functions/boos
6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-28 14:39

    The C++ standard says nothing about the performance of its requirements, only that the functionality should be there.

    These sort of questions about which is better or faster or more efficient are meaningless unless you state which implementation you're talking about. For example, the string handling in a certain version of a certain implementation of JavaScript was atrocious, but you can't extrapolate that to being a feature of the relevant standard.

    I would even go so far as to say it doesn't matter regardless of the implementation since the functionality provided by switch and std::map is different (although there's overlap).

    These sort of micro-optimizations are almost never necessary, in my opinion.

提交回复
热议问题