-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBenchEventVsCall.h
149 lines (123 loc) · 3.6 KB
/
BenchEventVsCall.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
//NOTE: intended to be included multiple times
struct Foo
{
Event<void(float)> evt;
};
#define CONN(X, Y) X##Y
#define GEN_STUFF(X) virtual void CONN(stuff, X)() {}
#define STUFF() GEN_STUFF(__COUNTER__)
#define STUFF_10() STUFF() STUFF() STUFF() STUFF() STUFF() STUFF() STUFF() STUFF() STUFF() STUFF()
#define STUFF_100() STUFF_10() STUFF_10() STUFF_10() STUFF_10() STUFF_10() STUFF_10() STUFF_10() STUFF_10() STUFF_10()
#define STUFF_1000() STUFF_100() STUFF_100() STUFF_100() STUFF_100() STUFF_100() STUFF_100() STUFF_100() STUFF_100() STUFF_100()
struct BarBase
{
virtual ~BarBase() {}
STUFF_1000()
virtual void vf(float delta) = 0;
STUFF_1000()
};
template<size_t I>
struct Bar : BarBase
{
void onEvt(float delta)
{
total += delta * ((rand() % 2) ? 1 : -1);
}
virtual void vf(float delta) override
{
onEvt(delta);
}
Listener<&Foo::evt, &Bar::onEvt> listener;
void connect(Foo* foo)
{
listener.connect(foo, this);
}
volatile float total = 0.f;
};
template<size_t I>
std::function<BarBase * (Foo*)> getFac()
{
return [](Foo* foo) -> BarBase*
{
auto b = new Bar<I>;
b->connect(foo);
return b;
};
}
template<size_t ... Is>
auto getFactoriesImpl(std::index_sequence<Is...>)
{
return std::array<std::function<BarBase * (Foo*)>, sizeof...(Is)> { getFac<Is>() ... };
}
auto getFactories()
{
return getFactoriesImpl(std::make_index_sequence<100>());
}
static auto fac = getFactories();
void test()
{
std::cout << "Event vs call (same class):\n";
{
std::vector<Bar<0>> bars;
Foo foo;
bars.resize(100'000);
for (auto& bar : bars)
bar.listener.connect(&foo, &bar);
double event_duration, call_duration;
{
auto start = std::chrono::high_resolution_clock::now();
foo.evt(0.001f);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end - start;
std::cout << "Event: " << diff.count() << " s\n";
event_duration = diff.count();
}
{
auto start = std::chrono::high_resolution_clock::now();
for (auto& bar : bars)
bar.onEvt(0.001f);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end - start;
std::cout << "Call: " << diff.count() << " s\n";
call_duration = diff.count();
}
{
auto start = std::chrono::high_resolution_clock::now();
for (auto& bar : bars)
bar.vf(0.001f);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end - start;
std::cout << "Virtual Call: " << diff.count() << " s\n";
call_duration = diff.count();
}
std::cout << "Event takes " << (int)(100 * event_duration / call_duration) << "% time of direct function calls \n";
}
std::cout << "Event vs virtual (random from 100 classes):\n";
{
std::vector<std::unique_ptr<BarBase>> bars;
Foo foo;
for (int i = 0; i < 100'000; i++)
bars.emplace_back(fac[rand() % 100](&foo));
double event_duration, call_duration;
{
auto start = std::chrono::high_resolution_clock::now();
foo.evt(0.001f);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end - start;
std::cout << "Event: " << diff.count() << " s\n";
event_duration = diff.count();
}
{
auto start = std::chrono::high_resolution_clock::now();
for (auto& bar : bars)
{
bar->vf(0.001f);
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end - start;
std::cout << "Virtual call: " << diff.count() << " s\n";
call_duration = diff.count();
}
std::cout << "Event takes " << (int)(100 * event_duration / call_duration) << "% time of virtual function calls \n";
}
}