Line data Source code
1 : //
2 : // DUNE DAQ modification notice:
3 : // This file has been modified from the original ATLAS oks_utils source for the DUNE DAQ project.
4 : // Fork baseline commit: c2e7dfc7 (2022-03-30).
5 : // Renamed since fork: yes (from examples/alloc.cpp to test/apps/alloc.cxx).
6 : //
7 :
8 : #include <sys/resource.h>
9 : #include <sys/time.h>
10 :
11 : #include <chrono>
12 : #include <iostream>
13 : #include <list>
14 :
15 : #include <boost/pool/pool_alloc.hpp>
16 :
17 : // Normal structure
18 :
19 : class Test
20 : {
21 : int i;
22 : void* p;
23 : };
24 :
25 : // Use Boost pool (with mutexes)
26 :
27 : class Test3 : public Test
28 : {
29 :
30 : public:
31 0 : void* operator new(size_t) { return boost::fast_pool_allocator<Test3>::allocate(); }
32 0 : void operator delete(void* ptr) { boost::fast_pool_allocator<Test3>::deallocate(reinterpret_cast<Test3*>(ptr)); }
33 : };
34 :
35 : // Use Boost pool (without mutexes)
36 :
37 : class Test4 : public Test
38 : {
39 :
40 : public:
41 0 : void* operator new(size_t)
42 : {
43 0 : return boost::fast_pool_allocator<Test4,
44 : boost::default_user_allocator_new_delete,
45 0 : boost::details::pool::null_mutex>::allocate();
46 : }
47 0 : void operator delete(void* ptr)
48 : {
49 0 : boost::fast_pool_allocator<Test4, boost::default_user_allocator_new_delete, boost::details::pool::null_mutex>::
50 0 : deallocate(reinterpret_cast<Test4*>(ptr));
51 0 : }
52 : };
53 :
54 : const size_t ArraySize = 20000000;
55 : const size_t ListSize = 20000000;
56 :
57 : int
58 0 : main()
59 : {
60 0 : size_t count;
61 :
62 0 : Test** t_array = new Test*[ArraySize];
63 0 : Test3** t3_array = new Test3*[ArraySize];
64 0 : Test4** t4_array = new Test4*[ArraySize];
65 :
66 0 : for (count = 0; count < ArraySize; count++) {
67 0 : t_array[count] = 0;
68 0 : t3_array[count] = 0;
69 0 : t4_array[count] = 0;
70 : }
71 :
72 0 : std::cout << "Create and delete " << ArraySize << " objects (" << sizeof(Test) << " bytes per object)\n";
73 :
74 : ////////////////////////////////////////////////////////////////////////////////
75 :
76 0 : auto tp = std::chrono::steady_clock::now();
77 :
78 0 : for (count = 0; count < ArraySize; count++)
79 0 : t_array[count] = new Test();
80 :
81 0 : for (count = 0; count < ArraySize; count++)
82 0 : delete t_array[count];
83 :
84 0 : std::cout << " * standard operators new and delete require "
85 0 : << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - tp).count() /
86 0 : 1000.
87 0 : << " seconds" << std::endl;
88 :
89 : ////////////////////////////////////////////////////////////////////////////////
90 :
91 0 : tp = std::chrono::steady_clock::now();
92 :
93 0 : for (count = 0; count < ArraySize; count++)
94 0 : t4_array[count] = new Test4();
95 :
96 0 : for (count = 0; count < ArraySize; count++)
97 0 : delete t4_array[count];
98 :
99 0 : std::cout << " * Boost operators new and delete require "
100 0 : << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - tp).count() /
101 0 : 1000.
102 0 : << " seconds (no mutexes)" << std::endl;
103 :
104 : ////////////////////////////////////////////////////////////////////////////////
105 :
106 0 : tp = std::chrono::steady_clock::now();
107 :
108 0 : for (count = 0; count < ArraySize; count++)
109 0 : t3_array[count] = new Test3();
110 :
111 0 : for (count = 0; count < ArraySize; count++)
112 0 : delete t3_array[count];
113 :
114 0 : std::cout << " * Boost operators new and delete require "
115 0 : << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - tp).count() /
116 0 : 1000.
117 0 : << " seconds (with mutexes)" << std::endl;
118 :
119 : ////////////////////////////////////////////////////////////////////////////////
120 :
121 0 : delete[] t_array;
122 0 : delete[] t3_array;
123 0 : delete[] t4_array;
124 :
125 : ////////////////////////////////////////////////////////////////////////////////
126 :
127 0 : std::cout << "Create and free list with " << ListSize << " integers\n";
128 :
129 0 : std::list<size_t> l;
130 :
131 0 : tp = std::chrono::steady_clock::now();
132 :
133 0 : for (count = 0; count < ListSize; count++)
134 0 : l.push_back(count);
135 :
136 0 : while (!l.empty())
137 0 : l.pop_front();
138 :
139 0 : std::cout << " * operation with list's standard allocator requires "
140 0 : << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - tp).count() /
141 0 : 1000.
142 0 : << " seconds" << std::endl;
143 :
144 : ////////////////////////////////////////////////////////////////////////////////
145 :
146 0 : std::list<
147 : size_t,
148 : boost::fast_pool_allocator<size_t, boost::default_user_allocator_new_delete, boost::details::pool::null_mutex>>
149 0 : l4;
150 :
151 0 : tp = std::chrono::steady_clock::now();
152 :
153 0 : for (count = 0; count < ListSize; count++)
154 0 : l4.push_back(count);
155 :
156 0 : while (!l4.empty())
157 0 : l4.pop_front();
158 :
159 0 : std::cout << " * operation with list's Boost allocator requires "
160 0 : << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - tp).count() /
161 0 : 1000.
162 0 : << " seconds (no mutexes)" << std::endl;
163 :
164 : ////////////////////////////////////////////////////////////////////////////////
165 :
166 0 : std::list<size_t, boost::fast_pool_allocator<size_t>> l3;
167 :
168 0 : tp = std::chrono::steady_clock::now();
169 :
170 0 : for (count = 0; count < ListSize; count++)
171 0 : l3.push_back(count);
172 :
173 0 : while (!l3.empty())
174 0 : l3.pop_front();
175 :
176 0 : std::cout << " * operation with list's Boost allocator requires "
177 0 : << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - tp).count() /
178 0 : 1000.
179 0 : << " seconds (with mutexes)" << std::endl;
180 :
181 : ////////////////////////////////////////////////////////////////////////////////
182 :
183 0 : return 0;
184 0 : }
|