How would you make this switch statement as fast as possible?

后端 未结 21 2230
别那么骄傲
别那么骄傲 2021-01-30 04:17

2009-12-04 UPDATE: For profiling results on a number of the suggestions posted here, see below!


The Question

Consider the following very

21条回答
  •  南方客
    南方客 (楼主)
    2021-01-30 05:03

    A couple of random thoughts, that may not all be applicable together:

    Switch on the first character in the string, rather than the string itself, and do a sub-switch for strings which can contain more than one letter?

    A hashtable would certainly guarantee O(1) retrieval, though it might not be faster for smaller numbers of comparisons.

    Don't use strings, use enums or something like a flyweight instead. Using strings in this case seems a bit fragile anyway...

    And if you really need it to be as fast as possible, why aren't you writing it in assembly? :)

提交回复
热议问题