8000 Example as performance test for Delegate vs. std::function · dok-net/arduino-esp8266@4fa0dbb · GitHub
[go: up one dir, main page]

Skip to content

Commit 4fa0dbb

Browse files
committed
Example as performance test for Delegate vs. std::function
1 parent 81ceebd commit 4fa0dbb

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
ESP8266 Delegate class template test by Dirk O. Kaar
3+
This example code is in the public domain
4+
*/
5+
6+
#include "Foo.h"
7+
#include <Delegate.h>
8+
#include <MultiDelegate.h>
9+
10+
constexpr long unsigned MAXCNT = 100000UL;
11+
const String TESTCASE = "F";
12+
const String LATENCY = "Latency/cycles = ";
13+
uint32_t cycles;
14+
uint32_t cnt;
15+
16+
enum TestCase { F0 = 0, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12 };
17+
TestCase testCases[] = { F0, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12 };
18+
19+
using Fp0 = bool (*)(Foo*, int);
20+
using Fp2 = bool(*)(int);
21+
22+
Foo* o0;
23+
Fp0 f0;
24+
Foo* o1;
25+
Fp0 f1;
26+
Delegate<bool(int)> f2;
27+
Delegate<bool(int), Foo*> f3;
28+
29+
std::function<bool(int)> f4;
30+
std::function<bool(int)> f5;
31+
32+
Delegate<bool(int)> f6;
33+
Delegate<bool(int)> f7;
34+
Delegate<bool(int)> f8;
35+
36+
Delegate<bool(int), Foo*> f9;
37+
Delegate<bool(int), Foo*> f10;
38+
Delegate<bool(int), Foo*> f11;
39+
Delegate<bool(int), Foo*> f12;
40+
41+
void set_f0(Fp0 _f, Foo* _o) { f0 = _f; o0 = _o; }
42+
void set_f1(Fp0 _f, Foo* _o) { f1 = _f; o1 = _o; }
43+
void set_f2(Fp2 _f) { f2 = { _f }; }
44+
void set_f3(Fp2 _f) { f3 = { _f }; }
45+
46+
void set_f4(const std::function<bool(int)>& _f) { f4 = _f; }
47+
void set_f5(const std::function<bool(int)>& _f) { f5 = _f; }
48+
49+
void set_f6(const Delegate<bool(int)>& _f) { f6 = _f; }
50+
void set_f7(const Delegate<bool(int)>& _f) { f7 = _f; }
51+
void set_f8(const Delegate<bool(int)>& _f) { f8 = _f; }
52+
53+
void set_f9(const Delegate<bool(int), Foo*>& _f) { f9 = _f; }
54+
void set_f10(const Delegate<bool(int), Foo*>& _f) { f10 = _f; }
55+
56+
void set_f11(const Delegate<bool(int), Foo*>& _f) { f11 = _f; }
57+
void set_f12(const Delegate<bool(int), Foo*>& _f) { f12 = _f; }
58+
59+
extern void testPrep();
60+
61+
void stopWatch()
62+
{
63+
if (MAXCNT == cnt)
64+
{
65+
Serial.print(LATENCY);
66+
Serial.println(cycles / MAXCNT);
67+
cycles = 0;
68+
cnt = 0;
69+
}
70+
}
71+
72+
void setup()
73+
{
74+
Serial.begin(115200);
75+
testPrep();
76+
77+
cycles = 0;
78+
cnt = 0;
79+
}
80+
81+
// Add the main program code into the continuous loop() function
82+
void loop()
83+
{
84+
for (auto tc : testCases) {
85+
Serial.print(TESTCASE);
86+
Serial.print(tc);
87+
Serial.print(": ");
88+
for (unsigned i = 0; i < MAXCNT; ++i)
89+
{
90+
auto start = ESP.getCycleCount();
91+
switch (tc) {
92+
case F0: f0(o0, 42); break;
93+
case F1: f1(o1, 42); break;
94+
case F2: f2(42); break; // { cbCPtr }
95+
case F3: f3(42); break; // { cbCPtr }
96+
97+
case F4: f4(42); break; // [o](int result) -> bool { return o->cb(result); }
98+
case F5: f5(42); break; // std::bind(Foo::cbwObj, o, std::placeholders::_1)
99+
100+
case F6: f6(42); break; // [o](int result) -> bool { return o->cb(result); }
101+
case F7: f7(42); break; // std::bind(Foo::cbwObj, o, std::placeholders::_1)
102+
case F8: f8(42); break; // [](int result) -> bool { return cbCPtr(result); }
103+
104+
case F9: f9(42); break; // [o](int result) -> bool { return o->cb(result); } <==== antipattern for Delegate, use f11 instead
105+
case F10: f10(42); break; // std::bind(Foo::cbwObj, o, std::placeholders::_1) <==== antipattern for Delegate, use f11 instead
106+
107+
case F11: f11(42); break; // [](Foo* o, int result) -> bool { return o->cb(result); }, o.get() })
108+
case F12: f12(42); break; // { Foo::cbwObj, o.get() }
109+
}
110+
cycles += (ESP.getCycleCount() - start);
111+
stopWatch();
112+
}
113+
yield();
114+
}
115+
delay(16000);
116+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
ESP8266 Delegate class template test by Dirk O. Kaar
3+
This example code is in the public domain
4+
*/
5+
6+
#include <ctype.h>
7+
8+
extern uint32_t cnt;
9+
10+
struct Foo {
11+
int val;
12+
bool cb(int result) { val = result; ++cnt; return true; }
13+
static bool cbwObj(Foo* obj, int result) { return ((Foo*)obj)->cb(result); }
14+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
ESP8266 Delegate class template test by Dirk O. Kaar
3+
This example code is in the public domain
4+
*/
5+
6+
#include "Foo.h"
7+
#include <Delegate.h>
8+
#include <memory>
9+
10+
extern void stopwatch();
11+
12+
std::shared_ptr<Foo> oPtr(new Foo());
13+
bool inline cbCPtr(int result) { return oPtr->cb(result); }
14+
15+
16+
extern void set_f0(bool(*_f)(Foo*, int), Foo* _o);
17+
extern void set_f1(bool(*_f)(Foo*, int), Foo* _o);
18+
extern void set_f2(bool(*)(int result));
19+
extern void set_f3(bool(*)(int result));
20+
21+
extern void set_f4(const std::function<bool(int)>& f);
22+
extern void set_f5(const std::function<bool(int)>& f);
23+
24+
extern void set_f6(const Delegate<bool(int)>&);
25+
extern void set_f7(const Delegate<bool(int)>&);
26+
extern void set_f8(const Delegate<bool(int)>&);
27+
28+
extern void set_f9(const Delegate<bool(int), Foo*>& f);
29+
extern void set_f10(const Delegate<bool(int), Foo*>& f);
30+
31+
extern void set_f11(const Delegate<bool(int), Foo*>& f);
32+
extern void set_f12(const Delegate<bool(int), Foo*>& f);
33+
34+
void testPrep() {
35+
std::shared_ptr<Foo> o(oPtr);
36+
set_f0(Foo::cbwObj, o.get());
37+
set_f1([](Foo* o, int result) -> bool { return o->cb(result); }, o.get());
38+
set_f2(cbCPtr);
39+
set_f3(cbCPtr);
40+
41+
set_f4([o](int result) -> bool { return o->cb(result); });
42+
set_f5(std::bind(Foo::cbwObj, o.get(), std::placeholders::_1));
43+
44+
set_f6([o](int result) -> bool { return o->cb(result); });
45+
set_f7(std::bind(Foo::cbwObj, o.get(), std::placeholders::_1));
46+
set_f8([](int result) -> bool { return cbCPtr(result); });
47+
48+
set_f9([o](int result) -> bool { return o->cb(result); });
49+
set_f10(std::bind(Foo::cbwObj, o.get(), std::placeholders::_1));
50+
51+
set_f11({ [](Foo* o, int result) -> bool { return o->cb(result); }, o.get() }); // fast calling!
52+
set_f12({ Foo::cbwObj, o.get() }); // fast calling!
53+
}

0 commit comments

Comments
 (0)
0