aboutsummaryrefslogtreecommitdiffstats
path: root/main.cpp
blob: 0d77c8dbc8fa336a73cd1d5bdf6fbf5e4fc81bc9 (plain)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <iostream>
#include <string>
#include <ctime>
#include <functional>
#include <iomanip>

#include "entities.hpp"

#include <entityx/entityx.h>
#include <entityx/deps/Dependencies.h>

using TestFunc = std::function<void(void)>;
constexpr unsigned int testCount = 100;

void test(const std::string& t, TestFunc ours, TestFunc theirs)
{
	auto run = [](auto f) {
		clock_t avg = 0;
		for (unsigned int i = 0; i < testCount; i++) {
			auto start = clock();
			f();
			auto diff = clock() - start;
			avg += diff;
		}
		auto diff = avg / testCount;
		std::cout << "Test took " <<
			diff << " ticks (" << diff / static_cast<double>(CLOCKS_PER_SEC) <<
			"ms).\n";
		return diff;
	};

	std::cout << std::fixed << std::setprecision(6);
	std::cout << "==============================================\n";
	std::cout << "Running test: " << t << "\n";

	std::cout << "Ours:\n";
	auto o = run(ours);

	std::cout << "Theirs:\n";
	auto p = run(theirs);

	auto percent = (o - p) / static_cast<float>(p);
	std::cout << "\nDIFFERENCE: " << o - p << " ticks (" <<
		std::setprecision(2) << percent << "%).\n\n";
}

struct Position : public Component {
	float x, y;

	Position(float _x = 0, float _y = 0)
		: x(_x), y(_y) {}
};

struct Velocity : public Component {
	float x, y;
	Velocity(float _x = 0, float _y = 0)
		: x(_x), y(_y) {}
};

int main(void)
{
	EntityManager em;

	entityx::EventManager ev;
	entityx::EntityManager xm (ev);
	entityx::SystemManager xsm (xm, ev);

	test("Creation500",
	[&em](){
		for (int i = 0; i < 500; i++)
			em.create();
	},
	[&xm](){
		for (int i = 0; i < 500; i++)
			xm.create();
	});

	test("Reset",
	[&em](){
		em.reset();
	},
	[&xm](){
		xm.reset();
	});

	test("Assign",
	[&em](){
		auto e = em.create();
		auto& pos = *e.assign<Position>(12, 34);

		//std::cout << pos.x << ' ' << pos.y << '\n';
		pos.x = 56, pos.y = 16;
		//std::cout << pos.x << ' ' << pos.y << '\n';
	},
	[&xm](){
		auto e = xm.create();
		auto pos = e.assign<Position>(12, 34);

		//std::cout << pos->x << ' ' << pos->y << '\n';
		pos->x = 56, pos->y = 16;
		//std::cout << pos->x << ' ' << pos->y << '\n';

	});

	test("HasComponent",
	[&em](){
		auto e = em.create();
		//std::cout << e.hasComponent<Position>() << '\n';
		e.assign<Position>();
		//std::cout << e.hasComponent<Position>() << '\n';
	},
	[&xm](){
		auto e = xm.create();
		//std::cout << e.has_component<Position>() << '\n';
		e.assign<Position>();
		//std::cout << e.has_component<Position>() << '\n';
	});

	test("Remove",
	[&em](){
		auto e = em.create();	
		//std::cout << e.hasComponent<Position>() << '\n';
		e.assign<Position>();
		//std::cout << e.hasComponent<Position>() << '\n';
		e.remove<Position>();
		//std::cout << e.hasComponent<Position>() << '\n';
	},
	[&xm](){
		auto e = xm.create();	
		//std::cout << e.has_component<Position>() << '\n';
		e.assign<Position>();
		//std::cout << e.has_component<Position>() << '\n';
		e.remove<Position>();
		//std::cout << e.has_component<Position>() << '\n';
	});

	/*test("EachAll", [&em](){
		em.reset();
		em.create(), em.create(), em.create();
		int i = 0;
		em.each([&i](Entity e) {
			std::cout << ++i << '\n';
		});
	});*/

	test("EachSpec",
	[&em](){
		em.reset();
		em.create(), em.create();
		auto e1 = em.create(); e1.assign<Position>(1, 3);
		em.create();
		auto e2 = em.create(); e2.assign<Position>(99);

		em.each<Position>([](Entity e) {
			auto& pos = *e.component<Position>();
			//std::cout << pos.x << ' ' << pos.y << '\n';
		});
	},
	[&xm](){
		xm.reset();
		xm.create(), xm.create();
		auto e1 = xm.create(); e1.assign<Position>(1, 3);
		xm.create();
		auto e2 = xm.create(); e2.assign<Position>(99);

		xm.each<Position>([](entityx::Entity e, Position& pos) {
			//std::cout << pos.x << ' ' << pos.y << '\n';
		});
	});

	test("Loop100",
	[&em](){
		for (int i = 100; i--;) {
			auto e = em.create();
			e.assign<Position>(i, i);
		}
		em.each<Position>([](Entity e) {
			auto& pos = * e.component<Position>();
			pos.x += 5, pos.y -= 5;
		});
	},
	[&xm](){
		for (int i = 100; i--;) {
			auto e = xm.create();
			e.assign<Position>(i, i);
		}
		xm.each<Position>([](entityx::Entity e, Position pos) {
			pos.x += 5, pos.y -= 5;
		});
	});

	/*test("Dependency",
	[&em](){
		em.addDependency<Velocity, Position>();
		auto e = em.create();
		e.assign<Velocity>();
		std::cout << e.hasComponent<Position>() << e.hasComponent<Velocity>() << '\n';
	},
	[&xm, &xsm](){
		xsm.add<entityx::deps::Dependency<Velocity, Position>>();
		auto e = xm.create();
		e.assign<Velocity>();
		std::cout << e.has_component<Position>() << e.has_component<Velocity>() << '\n';
	});*/

	return 0;
}