C++ map insertion and lookup performance and storage overhead

前端 未结 8 2032
旧巷少年郎
旧巷少年郎 2021-02-02 10:47

I would like to store a mapping of an integer key to a float value in-memory.

I have roughly 130 million keys (and, accordingly, 130 million v

8条回答
  •  闹比i
    闹比i (楼主)
    2021-02-02 11:40

    A vector is absolutely going to kill a map here assuming you don't need to do insertions in the middle of the vector. I wrote a custom allocator to track memory usage, and here are the results in Visual Studio 2005:

    std::map:

    1.3 million insertions
    Total memory allocated: 29,859 KB
    Total blocks allocated: 1,274,001
    Total time: 17.5 seconds
    

    std::vector >:

    1.3 million insertions
    Total memory allocated: 12,303 KB
    Total blocks allocated: 1
    Total time: 0.88 seconds
    

    std::map is using over twice the storage and taking 20 times longer to insert all the items.

提交回复
热议问题