-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.h
173 lines (136 loc) · 3.42 KB
/
benchmark.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#pragma once
#include <string>
#include <johl/Arrays.h>
#include <random>
#include <iostream>
using johl::Arrays;
using johl::aligned;
using johl::detail::unused;
struct Vec4
{
float x;
float y;
float z;
float w;
const Vec4& operator+=(const Vec4& v)
{
x += v.x;
y += v.y;
z += v.z;
w += v.w;
return *this;
}
Vec4 operator*(float f) const
{
return Vec4{x*f, y*f, z*f, w*f};
}
};
struct Name
{
char value[32];
};
struct Transform
{
Vec4 position;
Vec4 velocity;
};
struct Entity
{
bool active;
unsigned id;
Vec4 position;
Vec4 velocity;
Name debugname;
};
inline Entity createEntity(std::mt19937& generator, float threshold)
{
std::uniform_real_distribution<float> dis(0.0, 1.0);
Entity e;
e.active = dis(generator) < threshold;
e.position = Vec4{dis(generator),dis(generator),dis(generator),dis(generator)};
e.velocity = Vec4{dis(generator),dis(generator),dis(generator),dis(generator)};
return e;
}
//=============================================================================
// EntityVector
//=============================================================================
using EntityVector = std::vector<Entity>;
inline void setup(int num, float active, EntityVector& container)
{
container.reserve(num);
std::mt19937 generator(0);
for(int i=0;i<num; ++i)
{
Entity e = createEntity(generator, active );
container.push_back(e);
}
}
inline void update(EntityVector& container)
{
const size_t size = container.size();
Entity* entities = container.data();
for(size_t i=0;i<size; ++i)
{
Entity& e = entities[i];
if(e.active)
{
e.position += e.velocity * 0.1f;
}
}
}
//=============================================================================
// EntityVector
//=============================================================================
using EntityArrays = johl::Arrays<bool, unsigned, johl::aligned<Vec4, 16>, aligned<Vec4, 16>, Name>;
inline void setup(int num, float active, EntityArrays& container)
{
container.reserve(num);
std::mt19937 generator(0);
for(int i=0;i<num; ++i)
{
Entity e = createEntity(generator, active);
container.append(e.active, e.id, e.position, e.velocity, e.debugname);
}
}
inline void update(EntityArrays& container)
{
const size_t size = container.size();
const bool* active = container.data<0>();
const Vec4* velocity = container.data<3>();
Vec4* position = container.data<2>();
for(size_t i=0;i<size; ++i)
{
if(active[i])
{
position[i] += velocity[i] * 0.1f;
}
}
}
//=============================================================================
// EntityArrays2
//=============================================================================
using EntityArrays2 = johl::Arrays<bool, unsigned, Transform, Name>;
inline void setup(int num, float active, EntityArrays2& container)
{
container.reserve(num);
std::mt19937 generator(0);
for(int i=0;i<num; ++i)
{
Entity e = createEntity(generator, active);
container.append(e.active, e.id, Transform{e.position, e.velocity}, e.debugname);
}
}
inline void update(EntityArrays2& container)
{
const size_t size = container.size();
const bool* active = container.data<0>();
Transform* transform = container.data<2>();
for(size_t i=0;i<size; ++i)
{
if(active[i])
{
Transform& t = transform[i];
t.position += t.velocity * 0.1f;
}
}
}