Created
May 13, 2019 23:47
-
-
Save zoecarver/eaed954648f754d778c53835daea1de6 to your computer and use it in GitHub Desktop.
Speed comparison of unordered_multiset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<chrono> | |
#include<unordered_set> | |
#include<iostream> | |
#include<cassert> | |
using namespace std; | |
using namespace std::chrono; | |
template <class _Value, class _Hash, class _Pred, class _Alloc> | |
bool | |
complex_perm(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x, | |
const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y) | |
{ | |
typedef typename unordered_multiset<_Value, _Hash, _Pred, _Alloc>::const_iterator const_iterator; | |
typedef pair<const_iterator, const_iterator> _EqRng; | |
for (const_iterator __i = __x.begin(), __ex = __x.end(); __i != __ex;) | |
{ | |
_EqRng __xeq = __x.equal_range(*__i); | |
_EqRng __yeq = __y.equal_range(*__i); | |
if (_VSTD::distance(__xeq.first, __xeq.second) != | |
_VSTD::distance(__yeq.first, __yeq.second) || | |
!_VSTD::is_permutation(__xeq.first, __xeq.second, __yeq.first)) | |
return false; | |
__i = __xeq.second; | |
} | |
return true; | |
} | |
void a () | |
{ | |
unordered_multiset<int> u1; | |
unordered_multiset<int> u2; | |
for (unsigned i = 0; i < 1000 * 1000; ++i) | |
{ | |
u1.insert(i); | |
u2.insert(i); | |
} | |
for (unsigned i = 0; i < 1000 * 10; ++i) | |
{ | |
u1.insert(i % 100); | |
u2.insert(i % 100); | |
} | |
high_resolution_clock::time_point t1 = high_resolution_clock::now(); | |
assert(is_permutation(u1.begin(), u1.end(), u2.begin())); | |
high_resolution_clock::time_point t2 = high_resolution_clock::now(); | |
cout << duration_cast<milliseconds>( t2 - t1 ).count() << endl; | |
} | |
void b () | |
{ | |
unordered_multiset<int> u1; | |
unordered_multiset<int> u2; | |
for (unsigned i = 0; i < 1000 * 1000; ++i) | |
{ | |
u1.insert(i); | |
u2.insert(i); | |
} | |
for (unsigned i = 0; i < 1000 * 10; ++i) | |
{ | |
u1.insert(i % 100); | |
u2.insert(i % 100); | |
} | |
high_resolution_clock::time_point t1 = high_resolution_clock::now(); | |
assert(complex_perm(u1, u2)); | |
high_resolution_clock::time_point t2 = high_resolution_clock::now(); | |
cout << duration_cast<milliseconds>( t2 - t1 ).count() << endl; | |
} | |
int main() | |
{ | |
a(); | |
b(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment