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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
// ----------------------------------------------------------------------------
// Copyright (C) 2002-2006 Marcin Kalicinski
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// For more information, see www.boost.org
// ----------------------------------------------------------------------------
#define _HAS_ITERATOR_DEBUGGING 0
#define _SILENCE_CXX17_OLD_ALLOCATOR_MEMBERS_DEPRECATION_WARNING
// -Wdeprecated-copy-with-user-provided-copy in boost/format/group.hpp
#if defined(__clang__) && defined(__has_warning)
# if __has_warning( "-Wdeprecated-copy-with-user-provided-copy" )
# pragma clang diagnostic ignored "-Wdeprecated-copy-with-user-provided-copy"
# endif
// clang 10..12 emits this instead
# if __has_warning( "-Wdeprecated-copy" )
# pragma clang diagnostic ignored "-Wdeprecated-copy"
# endif
#endif
#include <boost/property_tree/ptree.hpp>
#include <boost/format.hpp>
#include <boost/shared_array.hpp>
#include <iostream>
#include <ctime>
#include <algorithm>
#include <random>
using namespace std;
using namespace boost;
using namespace boost::property_tree;
string dummy;
vector<string> keys;
vector<string> shuffled_keys;
void prepare_keys(int size)
{
// Prepare keys
keys.clear();
for (int i = 0; i < size; ++i)
keys.push_back((boost::format("%d") % i).str());
shuffled_keys = keys;
// Seed the engine with default seed every time
std::shuffle(shuffled_keys.begin(), shuffled_keys.end(), std::mt19937());
}
void clock_push_back(int size)
{
prepare_keys(size);
int max_repeats = 1000000 / size;
shared_array<ptree> pt_array(new ptree[max_repeats]);
int n = 0;
clock_t t1 = clock(), t2 = t1;
do
{
if (n >= max_repeats)
break;
ptree &pt = pt_array[n];
for (int i = 0; i < size; ++i)
pt.push_back(ptree::value_type(shuffled_keys[i], ptree()));
t2 = clock();
++n;
} while (t2 - t1 < CLOCKS_PER_SEC);
cout << " push_back (" << size << "): " << double(t2 - t1) / CLOCKS_PER_SEC / n * 1000 << " ms\n";
}
void clock_find(int size)
{
prepare_keys(size);
ptree pt;
for (int i = 0; i < size; ++i)
pt.push_back(ptree::value_type(keys[i], ptree("data")));
int n = 0;
clock_t t1 = clock(), t2;
do
{
for (int i = 0; i < size; ++i)
pt.find(shuffled_keys[i]);
t2 = clock();
++n;
} while (t2 - t1 < CLOCKS_PER_SEC);
cout << " find (" << size << "): " << double(t2 - t1) / CLOCKS_PER_SEC / n * 1000 << " ms\n";
}
void clock_erase(int size)
{
prepare_keys(size);
int max_repeats = 100000 / size;
shared_array<ptree> pt_array(new ptree[max_repeats]);
for (int n = 0; n < max_repeats; ++n)
for (int i = 0; i < size; ++i)
pt_array[n].push_back(ptree::value_type(keys[i], ptree("data")));
int n = 0;
clock_t t1 = clock(), t2 = t1;
do
{
if (n >= max_repeats)
break;
ptree &pt = pt_array[n];
for (int i = 0; i < size; ++i)
pt.erase(shuffled_keys[i]);
t2 = clock();
++n;
} while (t2 - t1 < CLOCKS_PER_SEC);
cout << " erase (" << size << "): " << double(t2 - t1) / CLOCKS_PER_SEC / n * 1000 << " ms\n";
}
int main()
{
// push_back
clock_push_back(10);
clock_push_back(100);
clock_push_back(1000);
// erase
clock_erase(10);
clock_erase(100);
clock_erase(1000);
// find
clock_find(10);
clock_find(100);
clock_find(1000);
}
|