-
Notifications
You must be signed in to change notification settings - Fork 54
/
main.cpp
97 lines (83 loc) · 2.45 KB
/
main.cpp
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
#include <UECS/UECS.hpp>
using namespace Ubpa::UECS;
#include <chrono>
#include <iostream>
#include <memory>
#include <thread>
struct A { float val; };
struct B { float val; };
struct TestSystem {
static void OnUpdate(Schedule& schedule) {
schedule.RegisterEntityJob(
[](const A* a, B* b) {
// 256 floating-point operations
for (std::size_t i = 0; i < 256; i++)
b->val *= a->val;
}, "TestSystem");
}
};
int main() {
std::size_t numEntities = 65536;
std::size_t numUpdate = 144 * 10;
World w;
w.entityMngr.cmptTraits.Register<A, B>();
w.systemMngr.RegisterAndActivate<TestSystem>();
{ // ECS
auto t0 = std::chrono::high_resolution_clock::now();
for (std::size_t i = 0; i < numEntities; i++)
w.entityMngr.Create(Ubpa::TypeIDs_of<A, B>);
auto t1 = std::chrono::high_resolution_clock::now();
for (std::size_t i = 0; i < numUpdate; i++)
w.Update();
auto t2 = std::chrono::high_resolution_clock::now();
// G5400 : 2 cores 4 threads
// about 10s
// i5 8400 : 6 cores 6 threads
// about 6s
// i5 10400 : 6 cores 12 threads
// about 2.7s
auto d0 = t1 - t0;
auto d1 = t2 - t1;
std::cout << "create: " << d0.count() / 1000000000.0 << "s" << std::endl;
std::cout << "update: " << d1.count() / 1000000000.0 << "s" << std::endl;
}
{ // direct
auto t0 = std::chrono::high_resolution_clock::now();
std::vector<std::unique_ptr<A>> As;
std::vector<std::unique_ptr<B>> Bs;
for (std::size_t i = 0; i < numEntities; i++) {
auto a = std::make_unique<A>();
a->val = 1;
auto b = std::make_unique<B>();
b->val = 2;
As.push_back(std::move(a));
Bs.push_back(std::move(b));
}
auto t1 = std::chrono::high_resolution_clock::now();
size_t N = std::thread::hardware_concurrency();
for (std::size_t i = 0; i < numUpdate; i++) {
auto work = [&](std::size_t id) {
for (std::size_t j = id; j < numEntities; j += N) {
const auto& a = As[j];
const auto& b = Bs[j];
for (std::size_t k = 0; k < 256; k++) {
b->val *= a->val;
}
}
};
std::vector<std::thread> jobs;
for (std::size_t n = 0; n < N; n++)
jobs.emplace_back(work, n);
for (auto& job : jobs)
job.join();
}
auto t2 = std::chrono::high_resolution_clock::now();
// i5 10400 : 6 cores 12 threads
// about 13s
auto d0 = t1 - t0;
auto d1 = t2 - t1;
std::cout << "create: " << d0.count() / 1000000000.0 << "s" << std::endl;
std::cout << "update: " << d1.count() / 1000000000.0 << "s" << std::endl;
}
return 0;
}