Insertion and reading is faster than std::map. |
Are you sure about that? Your latest code has
O(N*M)
complexity for the initialisation of the map. Even though you use the same algorithm to initialise both your container and the
std::map
, it looks like an unfair test: you are forcing
std::map
to be initialised with
O(N*M)
code. It might be different if all the pairs were created first, then pushed into the map.
Consider that the STL containers / algorithms are highly optimised, it's generally not worth it to try to do better. You are trying to optimise
std::map
, but
std::unordered_map
is better anyway.
chrono::high_resolution_clock
is not the right clock for timing this: the high resolution part might be misleading in terms of you think you are getting something better, but it's a wall clock not a cpu clock. Use
std::clock
This container keeps keys vector sorted for use of lower_bound or else it would be uselessly slow.
|
std::map
is often implemented as a BST or a red/black tree, so it will always be sorted. So you have written code that does the same thing. Is your code better than what the
std::map
does? Is your code to keep it sorted better than putting it all in the vector, then sorting it with
std::sort
? And more importantly than anything else, as
Peter87 says: Is it better than
std::unordered_map
? I very much doubt it.
Also consider that there might be more work inserting into a red / black tree (self balancing BST), but that might be better in the long run for certain data sets. The self balancing is probably more efficient in the end than maintaining a sorted vector. Also, inserting into a vector (no matter how one does it) is not going to be good: all the remaining elements have to be moved along 1 position. So that is why insertion in the middle of a vector is O(n).
Edit:
It, however, does not attempt to sort to second paring and I believe that is where it gets it speed from.
|
std::map
doesn't do that either. In (key, value) the value is a value. But your value is a vector, why should a container presume that the vector needs to be sorted?
end Edit
Also this container shines where the container size becomes very large
|
What size did you test it with? 17576 isn't large, 1 or 10 million might be a better test. And your strings only have 3 char.
Sorry to pour really cold water all over your idea, hopefully it is helpful in the end :+)