Better use HashTable or switch case

前端 未结 3 1575
无人及你
无人及你 2021-01-07 13:58

I\'m not sure which one is better. I need to parse each character of an input string and get a replacing string for the character. For some objects all alphanumeric characte

相关标签:
3条回答
  • 2021-01-07 14:37

    Hashing is faster as you can directly access the 'encoded string'

    for example, If you assume that all characters are '9', so it will have to evaluate 8 if conditions before executing the right statement, each time you process a character.

    That's just a worst-case example while using the switch()

    0 讨论(0)
  • 2021-01-07 14:49

    The switch code does the same thing as the alternative, but it's much more readable which should be the main concern here.

    I would however use an abstract factory to get one of a number of different encoding implementations for different types of input.

    0 讨论(0)
  • 2021-01-07 14:54

    I'd use the HashTable, because the code is more readable and maintainable: you could one day decide to load the subtitution strings from an XML file, so that you won't change the code to change the mappings.

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