D
I had a little experiment here. ♪ ♪Randomly created vector from N Tyres with positive and negative values using different containers:size_t usingSet(const std::vector<int>& input)
{
std::set<unsigned> unique;
for(int item : input) {
unique.insert(abs(item));
}
return unique.size();
}
size_t usingHash(const std::vector<int>& input)
{
std::unordered_set<unsigned> unique;
unique.reserve(input.size());
for(int item : input) {
unique.insert(abs(item));
}
return unique.size();
}
size_t usingVec(const std::vector<int>& input)
{
std::vector<unsigned> uniq;
uniq.reserve(input.size());
for(int item : input) {
uniq.push_back(abs(item));
}
sort(uniq.begin(),uniq.end());
return unique(uniq.begin(),uniq.end()) - uniq.begin();
}
1,000 times we'll find the number of unique elements. It's time in microseconds, it's clear that a plus-minus... but it's good for evaluation. I don't think it's hard for me to round it up in my mind in a few milliseconds. Set Hash Vector
N = 10: 790 1256 253
N = 100: 8304 8243 1192
N = 1000: 130070 78354 27573
N = 10000: 1543452 757059 498093
N = 100000: 14545110 4232742 6182957
N = 1000000: 127603400 27039847 61045621
So the vector is starting to lose between 10 and 100,000 Hash. For small values, the hash loses even set'Uh, compared to 100 elements. Next. set I'm sure he's losing everything. O(N*ln N)but also because of the large number of redistributions of memory (member) reserve() He doesn't have a hex and a vector.As didn't come up with anything to say goodbye to the memory consumption, but it's clear that the vector will give it to everyone.Words, the saddest is setit doesn't make sense at all. With a small size of tens of thousands, it's best to take a vector, and then watch what's more important is speed or memory.All this on Visual C+ 2015, 32 bit.
For Visual C++ 2010, 32 bit the advantages of the vector are still brighter: And the Hash is losing, Set Hash Vector
N = 10: 813 1459 223
N = 100: 8688 10795 1448
N = 1000: 135905 102716 29086
N = 10000: 1623786 1068878 519483
N = 100000: 18334735 8822607 6316194
N = 1000000: 179540082 83667899 61899536
Who wants to repeat for other compilators - you are welcome. ♪ ♪By the way, the vector can still be optimised because we need to. Number unique elements - may not be used unique with his remembrance, but simply to go through and count the elements that neighbour doesn't have the same.Update♪ I assumed that the number of coincidences was small; I was corrected in the comments. I didn't do the number of unique elements in myser-- my hand's not lying, but I filled up the mass:for(int i = 0; i < N; ++i)
src.push_back((rand()%(N/5))*(i%2 ? 1 : -1));
I mean, there's plenty of coincidences, but still unique elements-- O(N)The results for 2015 are as follows: Set Hash Vector
N = 10: 279 760 229
N = 100: 2693 2785 1185
N = 1000: 53087 24940 26121
N = 10000: 806965 273205 472123
N = 100000: 12350458 3440321 6006227
The border was slightly downward, but the principle remained: set It's flying, but it's hitting the hesh up to 100, and it's starting to run around 1,000, not 10000.To the limit when all N Elements - units, results almost paradoxical: Set Hash Vector
N = 10: 200 895 219
N = 100: 888 1840 493
N = 1000: 7676 10020 2778
N = 10000: 73524 102685 25881
N = 100000: 733058 1167238 259100
N = 1000000: 7396809 11645557 3561249
vector, and only vector! Hash loses even. setHuh.All right, there's no time for experiments; who wants to - go on:Update2 - In his "Optimized C++" Gunterot says, unordered_set Although it has an effect, it is not exactly the one to be expected to read how it is praised: The hash table std::unordered_map is faster than std::map, but not by the order-of-magnitude difference that its reputation suggests♪