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 == 1000
33 #define REPEAT(X) REPEAT_1000(X, _)
34
35 #else
36 #error Multiplier not supported.
37 #endif
38
39 #define PLACEHOLDER
40
41 #define EVAL0(...) __VA_ARGS__
42 #define EVAL1(...) EVAL0(EVAL0(EVAL0(EVAL0(__VA_ARGS__))))
43 #define EVAL2(...) EVAL1(EVAL1(EVAL1(EVAL1(__VA_ARGS__))))
44 #define EVAL(...) EVAL2(EVAL2(EVAL2(EVAL2(__VA_ARGS__))))
45
46 #define META_REPEAT_10(R, X, I) \
47 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) \
48 R PLACEHOLDER(X, I##5) R PLACEHOLDER(X, I##6) R PLACEHOLDER(X, I##7) R PLACEHOLDER(X, I##8) \
49 R PLACEHOLDER(X, I##9)
50
51 #define REPEAT_1(X, I) X(I)
52
53 #define REPEAT_10(X, I) META_REPEAT_10(REPEAT_1, X, I)
54
55 #define REPEAT_100(X, I) META_REPEAT_10(REPEAT_10, X, I)
56
57 #define REPEAT_1000(X, I) META_REPEAT_10(REPEAT_100, X, I)
58
59 using namespace std;
60
61 #define DEFINITIONS(N) \
62 struct I##N { \
63 virtual ~I##N() = default; \
64 }; \
65 \
66 struct C##N : public I##N { \
67 virtual ~C##N() = default; \
68 };
69
70 #define ALLOCATE(N) C##N* c##N = new C##N();
71
72 #define DEALLOCATE(N) delete c##N;
73
EVAL(REPEAT (DEFINITIONS))74 EVAL(REPEAT(DEFINITIONS))
75
76 int main(int argc, const char* argv[]) {
77 if (argc != 2) {
78 std::cout << "Error: you need to specify the number of loops as argument." << std::endl;
79 return 1;
80 }
81 size_t num_loops = std::atoi(argv[1]);
82
83 std::chrono::high_resolution_clock::time_point start_time = std::chrono::high_resolution_clock::now();
84
85 for (size_t i = 0; i < num_loops; i++) {
86 EVAL(REPEAT(ALLOCATE))
87 EVAL(REPEAT(DEALLOCATE))
88 }
89 double totalTime =
90 std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::high_resolution_clock::now() - start_time)
91 .count();
92
93 std::cout << std::fixed;
94 std::cout << std::setprecision(15);
95 std::cout << "Total = " << totalTime * 1.0 / num_loops << std::endl;
96
97 return 0;
98 }
99