HashMap vs Switch statement performance

后端 未结 4 1709
梦谈多话
梦谈多话 2021-01-30 13:14

A HashMap essentially has O(1) performance while a switch state can have either O(1) or O(log(n)) depending on if the compiler uses a tableswitch or lookup switch.

Under

相关标签:
4条回答
  • 2021-01-30 13:40

    In your case, since you are using an Integer key for your HashMap and a plain 'int' for your switch statement, the best performing implementation will be the switch statement unless the number of passes through this section of code is very high (tens or hundreds of thousands).

    0 讨论(0)
  • 2021-01-30 13:56

    The accepted answer is wrong here.

    http://java-performance.info/string-switch-implementation/

    Switches will always be as fast as if not faster than hash maps. Switch statements are transformed into direct lookup tables. In the case of Integer values (ints, enums, shorts, longs) it is a direct lookup/jmp to the statement. There is no additional hashing that needs to happen. In the case of a String, it precomputes the string hash for the case statements and uses the input String's hashcode to determine where to jump. In the case of collision, it does an if/else chain. Now you might think "This is the same as HashMap, right?" But that isn't true. The hash code for the lookup is computed at compile time and it isn't reduced based on the number of elements (lower chance of collision).

    Switches have O(1) lookup, not O(n). (Ok, in truth for a small number of items, switches are turned into if/else statements. This provides better code locality and avoids additional memory lookups. However, for many items, switches are changed into the lookup table I mentioned above).

    You can read more about it here How does Java's switch work under the hood?

    0 讨论(0)
  • 2021-01-30 13:57

    If I have that kind of example I use Guava ImmutableMaps (sure You can use java 9 builder as well).

    private static final Map<String, String> EXAMPLE = ImmutableMaps.<String, String>>builder()
        .put("a", "100")
        .put("b", "200")
        .build(); 
    

    That way they are immutable and initated only once.

    Sometimes I use strategy pattern that way:

    private static final Map<String, Command> EXAMPLE = ImmutableMaps.<String, String>>builder()
        .put("a", new SomethingCool())
        .put("b", new BCool())
        .build(); 
    
    private static final Command DEFAULT= new DefCommand();
    

    Use:

    EXAMPLE.getOrDefault("a", DEFAULT).execute(); //java 8
    

    About performance just pick readability. You will thank me later (1 year later) :D.

    0 讨论(0)
  • 2021-01-30 14:00

    It depends:

    1. If there are a few items | fixed items. Using switch if you can ( worst case O(n))

    2. If there a a lot of items OR you want to add future items without modifying much code ---> Using hash-map ( access time is considered as constant time)

    3. For your case. You should not worry about performance because the different execution time is very small. Just focus on readability/maintainability of your code. Is it worth to optimize a simple case to improve a few nanoseconds?

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