Line data Source code
1 : #include <sys/time.h>
2 : #include <sys/resource.h>
3 :
4 : #include <chrono>
5 : #include <list>
6 : #include <iostream>
7 :
8 : #include <boost/pool/pool_alloc.hpp>
9 :
10 :
11 : // Normal structure
12 :
13 : class Test {
14 : int i;
15 : void * p;
16 : };
17 :
18 :
19 : // Use Boost pool (with mutexes)
20 :
21 : class Test3 : public Test {
22 :
23 : public:
24 0 : void * operator new(size_t) {return boost::fast_pool_allocator<Test3>::allocate();}
25 0 : void operator delete(void *ptr) {boost::fast_pool_allocator<Test3>::deallocate(reinterpret_cast<Test3*>(ptr));}
26 :
27 : };
28 :
29 :
30 : // Use Boost pool (without mutexes)
31 :
32 : class Test4 : public Test {
33 :
34 : public:
35 0 : void * operator new(size_t) {return boost::fast_pool_allocator<Test4, boost::default_user_allocator_new_delete, boost::details::pool::null_mutex>::allocate();}
36 0 : void operator delete(void *ptr) {boost::fast_pool_allocator<Test4, boost::default_user_allocator_new_delete, boost::details::pool::null_mutex>::deallocate(reinterpret_cast<Test4*>(ptr));}
37 : };
38 :
39 :
40 :
41 :
42 : const size_t ArraySize = 20000000;
43 : const size_t ListSize = 20000000;
44 :
45 :
46 0 : int main()
47 : {
48 0 : size_t count;
49 :
50 0 : Test ** t_array = new Test * [ArraySize];
51 0 : Test3 ** t3_array = new Test3 * [ArraySize];
52 0 : Test4 ** t4_array = new Test4 * [ArraySize];
53 :
54 0 : for(count=0; count < ArraySize; count++) {
55 0 : t_array[count] = 0;
56 0 : t3_array[count] = 0;
57 0 : t4_array[count] = 0;
58 : }
59 :
60 0 : std::cout << "Create and delete " << ArraySize << " objects (" << sizeof(Test) << " bytes per object)\n";
61 :
62 : ////////////////////////////////////////////////////////////////////////////////
63 :
64 0 : auto tp = std::chrono::steady_clock::now();
65 :
66 0 : for(count=0; count < ArraySize; count++)
67 0 : t_array[count] = new Test();
68 :
69 0 : for(count=0; count < ArraySize; count++)
70 0 : delete t_array[count];
71 :
72 0 : std::cout << " * standard operators new and delete require " << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now()-tp).count() / 1000. << " seconds" << std::endl;
73 :
74 :
75 : ////////////////////////////////////////////////////////////////////////////////
76 :
77 0 : tp = std::chrono::steady_clock::now();
78 :
79 0 : for(count=0; count < ArraySize; count++)
80 0 : t4_array[count] = new Test4();
81 :
82 0 : for(count=0; count < ArraySize; count++)
83 0 : delete t4_array[count];
84 :
85 0 : std::cout << " * Boost operators new and delete require " << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now()-tp).count() / 1000. << " seconds (no mutexes)" << std::endl;
86 :
87 :
88 : ////////////////////////////////////////////////////////////////////////////////
89 :
90 0 : tp = std::chrono::steady_clock::now();
91 :
92 0 : for(count=0; count < ArraySize; count++)
93 0 : t3_array[count] = new Test3();
94 :
95 0 : for(count=0; count < ArraySize; count++)
96 0 : delete t3_array[count];
97 :
98 0 : std::cout << " * Boost operators new and delete require " << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now()-tp).count() / 1000. << " seconds (with mutexes)" << std::endl;
99 :
100 : ////////////////////////////////////////////////////////////////////////////////
101 :
102 0 : delete [] t_array;
103 0 : delete [] t3_array;
104 0 : delete [] t4_array;
105 :
106 : ////////////////////////////////////////////////////////////////////////////////
107 :
108 0 : std::cout << "Create and free list with " << ListSize << " integers\n";
109 :
110 0 : std::list<size_t> l;
111 :
112 0 : tp = std::chrono::steady_clock::now();
113 :
114 0 : for(count=0; count < ListSize; count++)
115 0 : l.push_back(count);
116 :
117 0 : while(!l.empty()) l.pop_front();
118 :
119 0 : std::cout << " * operation with list's standard allocator requires " << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now()-tp).count() / 1000. << " seconds" << std::endl;
120 :
121 : ////////////////////////////////////////////////////////////////////////////////
122 :
123 0 : std::list<size_t, boost::fast_pool_allocator<size_t, boost::default_user_allocator_new_delete, boost::details::pool::null_mutex> > l4;
124 :
125 0 : tp = std::chrono::steady_clock::now();
126 :
127 0 : for(count=0; count < ListSize; count++)
128 0 : l4.push_back(count);
129 :
130 0 : while(!l4.empty()) l4.pop_front();
131 :
132 0 : std::cout << " * operation with list's Boost allocator requires " << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now()-tp).count() / 1000. << " seconds (no mutexes)" << std::endl;
133 :
134 : ////////////////////////////////////////////////////////////////////////////////
135 :
136 0 : std::list<size_t, boost::fast_pool_allocator<size_t> > l3;
137 :
138 0 : tp = std::chrono::steady_clock::now();
139 :
140 0 : for(count=0; count < ListSize; count++)
141 0 : l3.push_back(count);
142 :
143 0 : while(!l3.empty()) l3.pop_front();
144 :
145 0 : std::cout << " * operation with list's Boost allocator requires " << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now()-tp).count() / 1000. << " seconds (with mutexes)" << std::endl;
146 :
147 : ////////////////////////////////////////////////////////////////////////////////
148 :
149 0 : return 0;
150 0 : }
|