1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
struct Timer {
const std::clock_t begin;
Timer() : begin (std::clock()) {}
~Timer() {
const std::clock_t end = std::clock();
std::cout << double (end - begin) / CLOCKS_PER_SEC << " seconds." << std::endl;
};
};
template <typename ForwardIterator>
bool lessThan (const typename std::iterator_traits<ForwardIterator>::value_type& a, const typename std::iterator_traits<ForwardIterator>::value_type& b,
ForwardIterator first, ForwardIterator last) {
while (first != last) {
if (*first == a && *first != b)
return true;
if (*first == b)
return false;
++first;
}
throw std::logic_error ("Neither element found in the given the range.");
}
class Object {} *apple = new Object, *table = new Object, *chair = new Object, *bowl = new Object, *doll = new Object,
*banana = new Object, *house = new Object, *baby = new Object, *candy = new Object, *plate = new Object;
const std::list<Object*> master0 = {apple, table, chair, bowl, doll, banana, house, baby, candy, plate};
bool compare (Object* a, Object* b) {
return lessThan (a, b, master0.begin(), master0.end());
}
std::unordered_map<Object*, int> master1 = {
{apple, 1}, {table, 2}, {chair, 3}, {bowl, 4}, {doll, 5}, {banana, 6}, {house, 7}, {baby, 8}, {candy, 9}, {plate, 10}
};
bool comp_unordered_map (Object* a, Object* b) {
return master1[a] < master1[b];
}
template <typename T>
bool comp_unordered_mapT (const T& obj1, const T& obj2) {
return master1[obj1] < master1[obj2];
}
std::map<Object*, int> master2 = {
{apple, 1}, {table, 2}, {chair, 3}, {bowl, 4}, {doll, 5}, {banana, 6}, {house, 7}, {baby, 8}, {candy, 9}, {plate, 10}
};
bool comp_map (Object* a, Object* b) {
return master2[a] < master2[b];
}
int main() {
const long N = 1000000;
const std::vector<Object*> subset = {bowl, table, apple, plate, house};
std::vector<Object*> v = subset;
std::cout << "OP method:" << std::endl;
{
Timer timer;
for (int i = 0; i < N; i++)
{std::sort (v.begin(), v.end(), compare); v = subset;} // v reset after sort
}
std::cout << "\nUsing removal method:" << std::endl;
{
Timer timer;
for (int i = 0; i < N; i++) {
std::list<Object*> copy = master0;
for (Object* x : master0) {
if (std::find(copy.begin(), copy.end(), x) == copy.end())
copy.remove(x);
}
}
}
std::cout << "\nUsing std::unordered_map:" << std::endl;
{
Timer timer;
for (int i = 0; i < N; i++)
{std::sort (v.begin(), v.end(), comp_unordered_map); v = subset;}
}
std::cout << "\nUsing std::unordered_mapT:" << std::endl;
{
Timer timer;
for (int i = 0; i < N; i++)
{std::sort (v.begin(), v.end(), comp_unordered_mapT<Object*>); v = subset;}
}
std::cout << "\nUsing std::_map:" << std::endl;
{
Timer timer;
for (int i = 0; i < N; i++)
{std::sort (v.begin(), v.end(), comp_map); v = subset;}
}
}
/*
Output:
OP method:
0.531 seconds.
Using removal method:
1.844 seconds.
Using std::unordered_map:
1.546 seconds.
Using std::unordered_mapT:
1.547 seconds.
Using std::_map:
1.391 seconds.
*/
|