-
Notifications
You must be signed in to change notification settings - Fork 3
/
benchmark.h
123 lines (101 loc) · 2.79 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
#pragma once
#include <chrono>
#include <vector>
#include "seecs.h"
class Timer {
private:
using Clock = std::chrono::steady_clock;
using Duration = Clock::duration;
using TimePoint = Clock::time_point;
TimePoint m_start;
public:
Timer() {
Reset();
}
// Return time in seconds since timer was last reset
float Elapsed() {
Duration duration = std::chrono::duration_cast<std::chrono::nanoseconds>(Clock::now() - m_start);
return duration.count() * 1e-9;
}
void Reset() {
m_start = Clock::now();
}
};
template <typename T>
struct Dummy {
T data;
};
inline void RunBenchmark(const std::size_t I) {
using namespace seecs;
Timer t;
ECS ecs;
std::vector<EntityID> ids;
ids.resize(I);
SEECS_MSG("Running 'creation' benchmark [" << I << "] entities");
t.Reset();
for (size_t i = 0; i < I; i++)
ids[i] = ecs.CreateEntity();
float elapsed = t.Elapsed();
SEECS_MSG(" - " << elapsed << "s");
t.Reset();
SEECS_MSG("Running 'add component' benchmark [" << I << "] entities");
t.Reset();
for (size_t i = 0; i < I; i++) {
ecs.Add<Dummy<int>>(ids[i], {});
}
elapsed = t.Elapsed();
SEECS_MSG(" - " << elapsed << "s");
t.Reset();
SEECS_MSG("Running 'get component' benchmark [" << I << "] entities");
t.Reset();
for (size_t i = 0; i < I; i++) {
ecs.Get<Dummy<int>>(ids[i]);
}
elapsed = t.Elapsed();
SEECS_MSG(" - " << elapsed << "s");
t.Reset();
SEECS_MSG("Running 'remove component' benchmark [" << I << "] entities");
t.Reset();
for (size_t i = 0; i < I; i++) {
ecs.Remove<Dummy<int>>(ids[i]);
}
elapsed = t.Elapsed();
SEECS_MSG(" - " << elapsed << "s");
t.Reset();
SEECS_MSG("Running 'delete entity' benchmark [" << I << "] entities");
t.Reset();
for (size_t i = 0; i < I; i++) {
ecs.DeleteEntity(ids[i]);
}
elapsed = t.Elapsed();
SEECS_MSG(" - " << elapsed << "s");
t.Reset();
ecs.Reset();
for (size_t i = 0; i < I; i++) {
ids[i] = ecs.CreateEntity();
ecs.Add<Dummy<int>>(ids[i], {});
ecs.Add<Dummy<double>>(ids[i], {});
}
SEECS_MSG("Running 'foreach (2 component)' benchmark [" << I << "] entities");
auto view1 = ecs.View<Dummy<int>, Dummy<double>>();
t.Reset();
view1.ForEach([](Dummy<int>& _, Dummy<double>& __) {});
elapsed = t.Elapsed();
SEECS_MSG(" - " << elapsed << "s");
t.Reset();
ecs.Reset();
for (size_t i = 0; i < I; i++) {
ids[i] = ecs.CreateEntity();
ecs.Add<Dummy<int>>(ids[i], {});
ecs.Add<Dummy<double>>(ids[i], {});
ecs.Add<Dummy<long>>(ids[i], {});
ecs.Add<Dummy<float>>(ids[i], {});
}
SEECS_MSG("Running 'foreach (4 component)' benchmark [" << I << "] entities");
auto view2 = ecs.View<Dummy<int>, Dummy<double>, Dummy<long>, Dummy<float>>();
t.Reset();
view2.ForEach([](Dummy<int>& _, Dummy<double>& __, Dummy<long>& ___, Dummy<float>& ____) {});
elapsed = t.Elapsed();
SEECS_MSG(" - " << elapsed << "s");
t.Reset();
}