C++: What is faster - lookup in hashmap or switch statement?

前端 未结 9 1502
心在旅途
心在旅途 2021-02-02 08:55

I have a code pattern which translates one integer to another. Just like this:

int t(int value) {
    switch (value) {
        case 1: return const_1;
        ca         


        
9条回答
  •  遥遥无期
    2021-02-02 09:58

    A switch statement is going to be quicker than a look up in a hash map.

    However, a map is going to result in much more readable code if you ever change the mappings. You can easily do this with a map by reading the results in from a file. In a switch statement you'd have to change the code and recompile.

提交回复
热议问题