• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <boost/property_tree/ptree.hpp>
14 #include <boost/format.hpp>
15 #include <boost/shared_array.hpp>
16 #include <iostream>
17 #include <ctime>
18 #include <algorithm>
19 
20 using namespace std;
21 using namespace boost;
22 using namespace boost::property_tree;
23 
24 string dummy;
25 vector<string> keys;
26 vector<string> shuffled_keys;
27 
prepare_keys(int size)28 void prepare_keys(int size)
29 {
30     // Prepare keys
31     keys.clear();
32     for (int i = 0; i < size; ++i)
33         keys.push_back((format("%d") % i).str());
34     shuffled_keys = keys;
35     srand(0);
36     random_shuffle(shuffled_keys.begin(), shuffled_keys.end());
37 }
38 
clock_push_back(int size)39 void clock_push_back(int size)
40 {
41     prepare_keys(size);
42     int max_repeats = 1000000 / size;
43     shared_array<ptree> pt_array(new ptree[max_repeats]);
44 
45     int n = 0;
46     clock_t t1 = clock(), t2;
47     do
48     {
49         if (n >= max_repeats)
50             break;
51         ptree &pt = pt_array[n];
52         for (int i = 0; i < size; ++i)
53             pt.push_back(ptree::value_type(shuffled_keys[i], ptree()));
54         t2 = clock();
55         ++n;
56     } while (t2 - t1 < CLOCKS_PER_SEC);
57 
58     cout << "  push_back (" << size << "): " << double(t2 - t1) / CLOCKS_PER_SEC / n * 1000 << " ms\n";
59 
60 }
61 
clock_find(int size)62 void clock_find(int size)
63 {
64     prepare_keys(size);
65 
66     ptree pt;
67     for (int i = 0; i < size; ++i)
68         pt.push_back(ptree::value_type(keys[i], ptree("data")));
69 
70     int n = 0;
71     clock_t t1 = clock(), t2;
72     do
73     {
74         for (int i = 0; i < size; ++i)
75             pt.find(shuffled_keys[i]);
76         t2 = clock();
77         ++n;
78     } while (t2 - t1 < CLOCKS_PER_SEC);
79 
80     cout << "  find (" << size << "): " << double(t2 - t1) / CLOCKS_PER_SEC / n * 1000 << " ms\n";
81 
82 }
83 
clock_erase(int size)84 void clock_erase(int size)
85 {
86     prepare_keys(size);
87 
88     int max_repeats = 100000 / size;
89     shared_array<ptree> pt_array(new ptree[max_repeats]);
90 
91     ptree pt;
92     for (int n = 0; n < max_repeats; ++n)
93         for (int i = 0; i < size; ++i)
94             pt_array[n].push_back(ptree::value_type(keys[i], ptree("data")));
95 
96     int n = 0;
97     clock_t t1 = clock(), t2;
98     do
99     {
100         if (n >= max_repeats)
101             break;
102         ptree &pt = pt_array[n];
103         for (int i = 0; i < size; ++i)
104             pt.erase(shuffled_keys[i]);
105         t2 = clock();
106         ++n;
107     } while (t2 - t1 < CLOCKS_PER_SEC);
108 
109     cout << "  erase (" << size << "): " << double(t2 - t1) / CLOCKS_PER_SEC / n * 1000 << " ms\n";
110 }
111 
main()112 int main()
113 {
114 
115     // push_back
116     clock_push_back(10);
117     clock_push_back(100);
118     clock_push_back(1000);
119 
120     // erase
121     clock_erase(10);
122     clock_erase(100);
123     clock_erase(1000);
124 
125     // find
126     clock_find(10);
127     clock_find(100);
128     clock_find(1000);
129 
130 }
131