| 1 | // ---------------------------------------------------------------------------- |
| 2 | // Copyright (C) 2002-2006 Marcin Kalicinski |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. |
| 5 | // (See accompanying file LICENSE_1_0.txt or copy at |
| 6 | // http://www.boost.org/LICENSE_1_0.txt) |
| 7 | // |
| 8 | // For more information, see www.boost.org |
| 9 | // ---------------------------------------------------------------------------- |
| 10 | |
| 11 | #define _HAS_ITERATOR_DEBUGGING 0 |
| 12 | |
| 13 | #define _SILENCE_CXX17_OLD_ALLOCATOR_MEMBERS_DEPRECATION_WARNING |
| 14 | |
| 15 | // -Wdeprecated-copy-with-user-provided-copy in boost/format/group.hpp |
| 16 | |
| 17 | #if defined(__clang__) && defined(__has_warning) |
| 18 | # if __has_warning( "-Wdeprecated-copy-with-user-provided-copy" ) |
| 19 | # pragma clang diagnostic ignored "-Wdeprecated-copy-with-user-provided-copy" |
| 20 | # endif |
| 21 | // clang 10..12 emits this instead |
| 22 | # if __has_warning( "-Wdeprecated-copy" ) |
| 23 | # pragma clang diagnostic ignored "-Wdeprecated-copy" |
| 24 | # endif |
| 25 | #endif |
| 26 | |
| 27 | #include <boost/property_tree/ptree.hpp> |
| 28 | #include <boost/format.hpp> |
| 29 | #include <boost/shared_array.hpp> |
| 30 | #include <iostream> |
| 31 | #include <ctime> |
| 32 | #include <algorithm> |
| 33 | #include <random> |
| 34 | |
| 35 | using namespace std; |
| 36 | using namespace boost; |
| 37 | using namespace boost::property_tree; |
| 38 | |
| 39 | string dummy; |
| 40 | vector<string> keys; |
| 41 | vector<string> shuffled_keys; |
| 42 | |
| 43 | void prepare_keys(int size) |
| 44 | { |
| 45 | // Prepare keys |
| 46 | keys.clear(); |
| 47 | for (int i = 0; i < size; ++i) |
| 48 | keys.push_back(x: (boost::format("%d" ) % i).str()); |
| 49 | shuffled_keys = keys; |
| 50 | // Seed the engine with default seed every time |
| 51 | std::shuffle(first: shuffled_keys.begin(), last: shuffled_keys.end(), g: std::mt19937()); |
| 52 | } |
| 53 | |
| 54 | void clock_push_back(int size) |
| 55 | { |
| 56 | prepare_keys(size); |
| 57 | int max_repeats = 1000000 / size; |
| 58 | shared_array<ptree> pt_array(new ptree[max_repeats]); |
| 59 | |
| 60 | int n = 0; |
| 61 | clock_t t1 = clock(), t2 = t1; |
| 62 | do |
| 63 | { |
| 64 | if (n >= max_repeats) |
| 65 | break; |
| 66 | ptree &pt = pt_array[n]; |
| 67 | for (int i = 0; i < size; ++i) |
| 68 | pt.push_back(value: ptree::value_type(shuffled_keys[i], ptree())); |
| 69 | t2 = clock(); |
| 70 | ++n; |
| 71 | } while (t2 - t1 < CLOCKS_PER_SEC); |
| 72 | |
| 73 | cout << " push_back (" << size << "): " << double(t2 - t1) / CLOCKS_PER_SEC / n * 1000 << " ms\n" ; |
| 74 | |
| 75 | } |
| 76 | |
| 77 | void clock_find(int size) |
| 78 | { |
| 79 | prepare_keys(size); |
| 80 | |
| 81 | ptree pt; |
| 82 | for (int i = 0; i < size; ++i) |
| 83 | pt.push_back(value: ptree::value_type(keys[i], ptree("data" ))); |
| 84 | |
| 85 | int n = 0; |
| 86 | clock_t t1 = clock(), t2; |
| 87 | do |
| 88 | { |
| 89 | for (int i = 0; i < size; ++i) |
| 90 | pt.find(key: shuffled_keys[i]); |
| 91 | t2 = clock(); |
| 92 | ++n; |
| 93 | } while (t2 - t1 < CLOCKS_PER_SEC); |
| 94 | |
| 95 | cout << " find (" << size << "): " << double(t2 - t1) / CLOCKS_PER_SEC / n * 1000 << " ms\n" ; |
| 96 | |
| 97 | } |
| 98 | |
| 99 | void clock_erase(int size) |
| 100 | { |
| 101 | prepare_keys(size); |
| 102 | |
| 103 | int max_repeats = 100000 / size; |
| 104 | shared_array<ptree> pt_array(new ptree[max_repeats]); |
| 105 | |
| 106 | for (int n = 0; n < max_repeats; ++n) |
| 107 | for (int i = 0; i < size; ++i) |
| 108 | pt_array[n].push_back(value: ptree::value_type(keys[i], ptree("data" ))); |
| 109 | |
| 110 | int n = 0; |
| 111 | clock_t t1 = clock(), t2 = t1; |
| 112 | do |
| 113 | { |
| 114 | if (n >= max_repeats) |
| 115 | break; |
| 116 | ptree &pt = pt_array[n]; |
| 117 | for (int i = 0; i < size; ++i) |
| 118 | pt.erase(key: shuffled_keys[i]); |
| 119 | t2 = clock(); |
| 120 | ++n; |
| 121 | } while (t2 - t1 < CLOCKS_PER_SEC); |
| 122 | |
| 123 | cout << " erase (" << size << "): " << double(t2 - t1) / CLOCKS_PER_SEC / n * 1000 << " ms\n" ; |
| 124 | } |
| 125 | |
| 126 | int main() |
| 127 | { |
| 128 | |
| 129 | // push_back |
| 130 | clock_push_back(size: 10); |
| 131 | clock_push_back(size: 100); |
| 132 | clock_push_back(size: 1000); |
| 133 | |
| 134 | // erase |
| 135 | clock_erase(size: 10); |
| 136 | clock_erase(size: 100); |
| 137 | clock_erase(size: 1000); |
| 138 | |
| 139 | // find |
| 140 | clock_find(size: 10); |
| 141 | clock_find(size: 100); |
| 142 | clock_find(size: 1000); |
| 143 | |
| 144 | } |
| 145 | |