When does using a std::multimap make sense

后端 未结 3 1937
天命终不由人
天命终不由人 2021-01-31 02:55

I am currently experimenting on some usage of stl-datastructures. However I am still not sure when to use which one and when to use a certain combination. Currently I am trying

3条回答
  •  被撕碎了的回忆
    2021-01-31 03:16

    You have forgotten one very important alternative: not all sequences are created equal.

    Especially, why a vector and not a deque or a list ?

    Using list

    A std::map > should perform roughly equivalently to a std::multimap since list is node based as well.

    Using deque

    A deque is the default container to use when you don't know for which to go and do not have any special requirement.

    With regard to the vector, you trade up some read speed (not much) for faster push and pop operations.

    Using a deque instead, and some obvious optimizations, I get:

    const uint32_t num_partitions = 100000;
    const size_t num_elements =     500000;
    
    Filling std::multimap: 360000 ticks
    Filling MyMumap:       530000 ticks
    
    Reading std::multimap: 70000 ticks (0)
    Reading MyMumap:       30000 ticks (0)
    

    Or in the "bad" case:

    const uint32_t num_partitions = 100000;
    const size_t num_elements =     200000;
    
    Filling std::multimap: 100000 ticks
    Filling MyMumap:       240000 ticks
    
    Reading std::multimap: 30000 ticks (0)
    Reading MyMumap:       10000 ticks (0)
    

    Thus reading is unconditionally faster, but filling is also way slower.

提交回复
热议问题