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