Click here to Skip to main content
16,018,092 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am looking for a map implementation which is optimized than std::unordered_map in C++ interms of CPU and memory usage.

What I have tried:

I had tried the ankerl::unordered_dense_map ,few sites suggested its the fastest,but with my profiling I found this one using more CPU than my std::unordered_map.

my code:(I profiled the method insert(),which resulted in std::unordered_map using 57% and ankerl map using 63% of CPU)
C++
#include <ankerl\unordered_dense.h>
#include <iostream>


void insert()
{
     // ankerl::unordered_dense::map<int, int=""> map;
     std::unordered_map<int, int=""> map;
    for (int i = 0; i < 1000000; ++i) {
        map.insert({ i, i });
    }
}

int main() {
    insert();
    return 0;
}
Posted
Updated 3-May-24 0:46am
v2
Comments
Richard Deeming 3-May-24 6:51am    
That's the thing about "fastest" code - it tends to use more CPU.

Based on your description, you don't want the "fastest" code; you want the "most optimized" code.

But honestly, inserting one million already-sorted integers into a map is not something you're likely to be doing in a real-world application. So it's not much use as a test to compare different map implementations.
Rick York 3-May-24 13:08pm    
I have used the unordered_map several times and I consider it to be very, very high performance. I have a program that puts 12K file names in a map where the first few dozen characters were all the same and it read them all from the directory in less than twenty milliseconds. I can't think of a reason for needing better performance than that. I really couldn't care less how much of the CPU it uses to do that and I can't think of a good reason why that should matter.
CPallini 6-May-24 2:30am    
I would use an ordinary map in your scenario.

1 solution

For a comprehensive evaluation of a program's performance, all relevant criteria must first be defined and prioritized. In addition to CPU utilization and memory consumption, for example, computing time is an important factor. Here it seems as if the questioner is carrying out the evaluation without taking the total computing time into account. This does not seem to me to be a sensible evaluation, unless you deliberately want to minimize the load on CPU and memory.

If you are looking for improved performance compared to std::unordered_map in terms of memory and cache efficiency, an unordered_dense::map would perhaps be an improvement. The comments of the question suggest that this is about optimizing CPU usage with similar performance to std::unordered_map.

To achieve this goal, I would first check whether more efficient algorithms and data structures could perhaps fulfill the requirement better than a map. Common alternatives could be arrays or vectors, sorted lists, hash tables or sparse arrays. Whether an alternative would bring an improvement in the context of your specific requirements would have to be determined with tests.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900