xref: /aosp_15_r20/external/google-fruit/extras/benchmark/new_delete_benchmark.cpp (revision a65addddcf69f38db5b288d787b6b7571a57bb8f)
1 /*
2  * Copyright 2014 Google Inc. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <chrono>
18 #include <ctime>
19 #include <iomanip>
20 #include <iostream>
21 #include <vector>
22 
23 #if MULTIPLIER == 1
24 #define REPEAT(X) REPEAT_1(X, _)
25 
26 #elif MULTIPLIER == 10
27 #define REPEAT(X) REPEAT_10(X, _)
28 
29 #elif MULTIPLIER == 100
30 #define REPEAT(X) REPEAT_100(X, _)
31 
32 #elif MULTIPLIER == 250
33 #define REPEAT(X) REPEAT_250(X, _)
34 
35 #elif MULTIPLIER == 1000
36 #define REPEAT(X) REPEAT_1000(X, _)
37 
38 #else
39 #error Multiplier not supported.
40 #endif
41 
42 #define PLACEHOLDER
43 
44 #define EVAL0(...) __VA_ARGS__
45 #define EVAL1(...) EVAL0(EVAL0(EVAL0(EVAL0(__VA_ARGS__))))
46 #define EVAL2(...) EVAL1(EVAL1(EVAL1(EVAL1(__VA_ARGS__))))
47 #define EVAL(...) EVAL2(EVAL2(EVAL2(EVAL2(__VA_ARGS__))))
48 
49 #define META_REPEAT_10(R, X, I)                                                                                        \
50   R PLACEHOLDER(X, I##0) R PLACEHOLDER(X, I##1) R PLACEHOLDER(X, I##2) R PLACEHOLDER(X, I##3) R PLACEHOLDER(X, I##4)   \
51       R PLACEHOLDER(X, I##5) R PLACEHOLDER(X, I##6) R PLACEHOLDER(X, I##7) R PLACEHOLDER(X, I##8)                      \
52           R PLACEHOLDER(X, I##9)
53 
54 #define META_REPEAT_5(R, X, I)                                                                                        \
55   R PLACEHOLDER(X, I##0) R PLACEHOLDER(X, I##1) R PLACEHOLDER(X, I##2) R PLACEHOLDER(X, I##3) R PLACEHOLDER(X, I##4)
56 
57 #define REPEAT_1(X, I) X(I)
58 
59 #define REPEAT_5(X, I) META_REPEAT_5(REPEAT_1, X, I)
60 
61 #define REPEAT_10(X, I) META_REPEAT_10(REPEAT_1, X, I)
62 
63 #define REPEAT_25(X, I) META_REPEAT_5(REPEAT_5, X, I)
64 
65 #define REPEAT_100(X, I) META_REPEAT_10(REPEAT_10, X, I)
66 
67 #define REPEAT_250(X, I) META_REPEAT_10(REPEAT_25, X, I)
68 
69 #define REPEAT_1000(X, I) META_REPEAT_10(REPEAT_100, X, I)
70 
71 using namespace std;
72 
73 #define DEFINITIONS(N)                                                                                                 \
74   struct I##N {                                                                                                        \
75     virtual ~I##N() = default;                                                                                         \
76   };                                                                                                                   \
77                                                                                                                        \
78   struct C##N : public I##N {                                                                                          \
79     virtual ~C##N() = default;                                                                                         \
80   };
81 
82 #define ALLOCATE(N) C##N* c##N = new C##N();
83 
84 #define DEALLOCATE(N) delete c##N;
85 
EVAL(REPEAT (DEFINITIONS))86 EVAL(REPEAT(DEFINITIONS))
87 
88 int main(int argc, const char* argv[]) {
89   if (argc != 2) {
90     std::cout << "Error: you need to specify the number of loops as argument." << std::endl;
91     return 1;
92   }
93   size_t num_loops = std::atoi(argv[1]);
94 
95   std::chrono::high_resolution_clock::time_point start_time = std::chrono::high_resolution_clock::now();
96 
97   for (size_t i = 0; i < num_loops; i++) {
98     EVAL(REPEAT(ALLOCATE))
99     EVAL(REPEAT(DEALLOCATE))
100   }
101   double totalTime =
102       std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::high_resolution_clock::now() - start_time)
103           .count();
104 
105   std::cout << std::fixed;
106   std::cout << std::setprecision(15);
107   std::cout << "Total           = " << totalTime * 1.0 / num_loops << std::endl;
108 
109   return 0;
110 }
111