8000 Added preprocessor define NODELEGATE. Use it for side-by-side compari… · dok-net/arduino-esp8266@6aa1102 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6aa1102

Browse files
committed
Added preprocessor define NODELEGATE. Use it for side-by-side comparison of Delegate<> vs. std::function<>
1 parent 66d1704 commit 6aa1102

File tree

3 files changed

+106
-21
lines changed

3 files changed

+106
-21
lines changed

libraries/esp8266/examples/DelegatePerf/DelegatePerf.ino

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
*/
55

66
#include "Foo.h"
7+
#ifndef NODELEGATE
78
#include <Delegate.h>
89
#include <MultiDelegate.h>
10+
#else
11+
#include <functional>
12+
#endif
913

1014
constexpr long unsigned MAXCNT = 100000UL;
1115
const String TESTCASE = "F";
@@ -23,12 +27,18 @@ Foo* o0;
2327
Fp0 f0;
2428
Foo* o1;
2529
Fp0 f1;
30+
#ifndef NODELEGATE
2631
Delegate<bool(int)> f2;
2732
Delegate<bool(int), Foo*> f3;
33+
#else
34+
std::function<bool(int)> f2;
35+
std::function<bool(int)> f3;
36+
#endif
2837

2938
std::function<bool(int)> f4;
3039
std::function<bool(int)> f5;
3140

41+
#ifndef NODELEGATE
3242
Delegate<bool(int)> f6;
3343
Delegate<bool(int)> f7;
3444

@@ -39,6 +49,18 @@ Delegate<bool(int), Foo*> f10;
3949

4050
Delegate<bool(int), Foo*> f11;
4151
Delegate<bool(int), Foo*> f12;
52+
#else
53+
std::function<bool(int)> f6;
54+
std::function<bool(int)> f7;
55+
56+
std::function<bool(int)> f8;
57+
58+
std::function<bool(int)> f9;
59+
std::function<bool(int)> f10;
60+
61+
std::function<bool(int)> f11;
62+
std::function<bool(int)> f12;
63+
#endif
4264

4365
void set_f0(Fp0 _f, Foo* _o) {
4466
f0 = _f;
@@ -62,6 +84,7 @@ void set_f5(const std::function<bool(int)>& _f) {
6284
f5 = _f;
6385
}
6486

87+
#ifndef NODELEGATE
6588
void set_f6(const Delegate<bool(int)>& _f) {
6689
f6 = _f;
6790
}
@@ -86,6 +109,32 @@ void set_f11(const Delegate<bool(int), Foo*>& _f) {
86109
void set_f12(const Delegate<bool(int), Foo*>& _f) {
87110
f12 = _f;
88111
}
112+
#else
113+
void set_f6(const std::function<bool(int)>& _f) {
114+
f6 = _f;
115+
}
116+
void set_f7(const std::function<bool(int)>& _f) {
117+
f7 = _f;
118+
}
119+
120+
void set_f8(const std::function<bool(int)>& _f) {
121+
f8 = _f;
122+
}
123+
124+
void set_f9(const std::function<bool(int)>& _f) {
125+
f9 = _f;
126+
}
127+
void set_f10(const std::function<bool(int)>& _f) {
128+
f10 = _f;
129+
}
130+
131+
void set_f11(const std::function<bool(int)>& _f) {
132+
f11 = _f;
133+
}
134+
void set_f12(const std::function<bool(int)>& _f) {
135+
f12 = _f;
136+
}
137+
#endif
89138

90139
extern void testPrep();
91140

libraries/esp8266/examples/DelegatePerf/Foo.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
extern uint32_t cnt;
99

1010
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); }
11+
int val;
12+
bool cb(int result) {
13+
val = result;
14+
++cnt;
15+
return true;
16+
}
17+
static bool cbwObj(Foo* obj, int result) {
18+
return ((Foo*)obj)->cb(result);
19+
}
1420
};

libraries/esp8266/examples/DelegatePerf/TestPrep.cpp

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44
*/
55

66
#include "Foo.h"
7+
#ifndef NODELEGATE
78
#include <Delegate.h>
9+
#else
10+
#include <functional>
11+
#endif
812
#include <memory>
913

1014
extern void stopwatch();
1115

1216
std::shared_ptr<Foo> oPtr(new Foo());
13-
bool inline cbCPtr(int result) { return oPtr->cb(result); }
17+
bool inline cbCPtr(int result) {
18+
return oPtr->cb(result);
19+
}
1420

1521

1622
extern void set_f0(bool(*_f)(Foo*, int), Foo* _o);
@@ -21,6 +27,7 @@ extern void set_f3(bool(*)(int result));
2127
extern void set_f4(const std::function<bool(int)>& f);
2228
extern void set_f5(const std::function<bool(int)>& f);
2329

30+
#ifndef NODELEGATE
2431
extern void set_f6(const Delegate<bool(int)>&);
2532
extern void set_f7(const Delegate<bool(int)>&);
2633

@@ -31,25 +38,48 @@ extern void set_f10(const Delegate<bool(int), Foo*>& f);
3138

3239
extern void set_f11(const Delegate<bool(int), Foo*>& f);
3340
extern void set_f12(const Delegate<bool(int), Foo*>& f);
41+
#else
42+
extern void set_f6(const std::function<bool(int)>&);
43+
extern void set_f7(const std::function<bool(int)>&);
3444

35-
void testPrep() {
36-
std::shared_ptr<Foo> o(oPtr);
37-
set_f0(Foo::cbwObj, o.get());
38-
set_f1([](Foo* o, int result) -> bool { return o->cb(result); }, o.get());
39-
set_f2(cbCPtr);
40-
set_f3(cbCPtr);
41-
42-
set_f4([o](int result) -> bool { return o->cb(result); });
43-
set_f5(std::bind(Foo::cbwObj, o.get(), std::placeholders::_1));
45+
extern void set_f8(const std::function<bool(int)>&);
4446

45-
set_f6([o](int result) -> bool { return o->cb(result); });
46-
set_f7(std::bind(Foo::cbwObj, o.get(), std::placeholders::_1));
47+
extern void set_f9(const std::function<bool(int)>& f);
48+
extern void set_f10(const std::function<bool(int)>& f);
4749

48-
set_f8([](int result) -> bool { return cbCPtr(result); });
50+
extern void set_f11(const std::function<bool(int)>& f);
51+
extern void set_f12(const std::function<bool(int)>& f);
52+
#endif
4953

50-
set_f9([o](int result) -> bool { return o->cb(result); });
51-
set_f10(std::bind(Foo::cbwObj, o.get(), std::placeholders::_1));
52-
53-
set_f11({ [](Foo* o, int result) -> bool { return o->cb(result); }, o.get() }); // fast calling!
54-
set_f12({ Foo::cbwObj, o.get() }); // fast calling!
54+
void testPrep() {
55+
std::shared_ptr<Foo> o(oPtr);
56+
set_f0(Foo::cbwObj, o.get());
57+
set_f1([](Foo * o, int result) -> bool { return o->cb(result); }, o.get());
58+
set_f2(cbCPtr);
59+
set_f3(cbCPtr);
60+
61+
set_f4([o](int result) -> bool { return o->cb(result); });
62+
set_f5(std::bind(Foo::cbwObj, o.get(), std::placeholders::_1));
63+
64+
set_f6([o](int result) -> bool { return o->cb(result); });
65+
set_f7(std::bind(Foo::cbwObj, o.get(), std::placeholders::_1));
66+
67+
#ifndef NODELEGATE
68+
// hint to compiler to generate Delegate constructor for simple fp instead of functional
69+
using Fp2 = bool(*)(int);
70+
set_f8(static_cast<Fp2>([](int result) -> bool { return cbCPtr(result); }));
71+
#else
72+
set_f8([](int result) -> bool { return cbCPtr(result); });
73+
#endif
74+
75+
set_f9([o](int result) -> bool { return o->cb(result); });
76+
set_f10(std::bind(Foo::cbwObj, o.get(), std::placeholders::_1));
77+
78+
#ifndef NODELEGATE
79+
set_f11({ [](Foo * o, int result) -> bool { return o->cb(result); }, o.get() }); // fast calling!
80+
set_f12({ Foo::cbwObj, o.get() }); // fast calling!
81+
#else
82+
set_f11([o](int result) -> bool { return o->cb(result); });
83+
set_f12(std::bind(Foo::cbwObj, o.get(), std::placeholders::_1));
84+
#endif
5585
}

0 commit comments

Comments
 (0)
0