Debian Codegraph @

Tests/Recast/Bench_rcVector.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 #include <stdio.h>
2 #include <string.h>
3
4 #include "catch2/catch_all.hpp"
5
6 #include "Recast.h"
7 #include "RecastAlloc.h"
8 #include "RecastAssert.h"
9 #include <vector>
10
11 // TODO: Implement benchmarking for platforms other than posix.
12 #ifdef __unix__
13 #include <unistd.h>
14 #ifdef _POSIX_TIMERS
15 #include <time.h>
16 #include <stdint.h>
17
18 int64_t NowNanos() {
19 struct timespec tp;
20 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp);
21 return tp.tv_nsec + 1000000000LL * tp.tv_sec;
22 }
23
24 #define BM(name, iterations) \
25 struct BM_ ## name { \
26 static void Run() { \
27 int64_t begin_time = NowNanos(); \
28 for (int i = 0 ; i < iterations; i++) { \
29 Body(); \
30 } \
31 int64_t nanos = NowNanos() - begin_time; \
32 printf("BM_%-35s %ld iterations in %10ld nanos: %10.2f nanos/it\n", #name ":", (int64_t)iterations, nanos, double(nanos) / iterations); \
33 } \
34 static void Body(); \
35 }; \
36 TEST_CASE(#name) { \
37 BM_ ## name::Run(); \
38 } \
39 void BM_ ## name::Body()
40
41 const int64_t kNumLoops = 100;
42 const int64_t kNumInserts = 100000;
43
44 // Prevent compiler from eliding a calculation.
45 // TODO: Implement for MSVC.
46 template <typename T>
47 void DoNotOptimize(T* v) {
48 asm volatile ("" : "+r" (v));
49 }
50
51 BM(FlatArray_Push, kNumLoops)
52 {
53 int cap = 64;
54 int* v = (int*)rcAlloc(cap * sizeof(int), RC_ALLOC_TEMP);
55 for (int j = 0; j < kNumInserts; j++) {
56 if (j == cap) {
57 cap *= 2;
58 int* tmp = (int*)rcAlloc(sizeof(int) * cap, RC_ALLOC_TEMP);
59 memcpy(tmp, v, j * sizeof(int));
60 rcFree(v);
61 v = tmp;
62 }
63 v[j] = 2;
64 }
65
66 DoNotOptimize(v);
67 rcFree(v);
68 }
69 BM(FlatArray_Fill, kNumLoops)
70 {
71 int* v = (int*)rcAlloc(sizeof(int) * kNumInserts, RC_ALLOC_TEMP);
72 for (int j = 0; j < kNumInserts; j++) {
73 v[j] = 2;
74 }
75
76 DoNotOptimize(v);
77 rcFree(v);
78 }
79 BM(FlatArray_Memset, kNumLoops)
80 {
81 int* v = (int*)rcAlloc(sizeof(int) * kNumInserts, RC_ALLOC_TEMP);
82 memset(v, 0, kNumInserts * sizeof(int));
83
84 DoNotOptimize(v);
85 rcFree(v);
86 }
87
88 BM(rcVector_Push, kNumLoops)
89 {
90 rcTempVector<int> v;
91 for (int j = 0; j < kNumInserts; j++) {
92 v.push_back(2);
93 }
94 DoNotOptimize(v.data());
95 }
96 BM(rcVector_PushPreallocated, kNumLoops)
97 {
98 rcTempVector<int> v;
99 v.reserve(kNumInserts);
100 for (int j = 0; j < kNumInserts; j++) {
101 v.push_back(2);
102 }
103 DoNotOptimize(v.data());
104 }
105 BM(rcVector_Assign, kNumLoops)
106 {
107 rcTempVector<int> v;
108 v.assign(kNumInserts, 2);
109 DoNotOptimize(v.data());
110 }
111 BM(rcVector_AssignIndices, kNumLoops)
112 {
113 rcTempVector<int> v;
114 v.resize(kNumInserts);
115 for (int j = 0; j < kNumInserts; j++) {
116 v[j] = 2;
117 }
118 DoNotOptimize(v.data());
119 }
120 BM(rcVector_Resize, kNumLoops)
121 {
122 rcTempVector<int> v;
123 v.resize(kNumInserts, 2);
124 DoNotOptimize(v.data());
125 }
126
127 BM(stdvector_Push, kNumLoops)
128 {
129 std::vector<int> v;
130 for (int j = 0; j < kNumInserts; j++) {
131 v.push_back(2);
132 }
133 DoNotOptimize(v.data());
134 }
135 BM(stdvector_PushPreallocated, kNumLoops)
136 {
137 std::vector<int> v;
138 v.reserve(kNumInserts);
139 for (int j = 0; j < kNumInserts; j++) {
140 v.push_back(2);
141 }
142 DoNotOptimize(v.data());
143 }
144 BM(stdvector_Assign, kNumLoops)
145 {
146 std::vector<int> v;
147 v.assign(kNumInserts, 2);
148 DoNotOptimize(v.data());
149 }
150 BM(stdvector_AssignIndices, kNumLoops)
151 {
152 std::vector<int> v;
153 v.resize(kNumInserts);
154 for (int j = 0; j < kNumInserts; j++) {
155 v[j] = 2;
156 }
157 DoNotOptimize(v.data());
158 }
159 BM(stdvector_Resize, kNumLoops)
160 {
161 std::vector<int> v;
162 v.resize(kNumInserts, 2);
163 DoNotOptimize(v.data());
164 }
165
166 #undef BM
167 #endif // _POSIX_TIMERS
168 #endif // __unix__