17
17
#include < functional>
18
18
#include < vector>
19
19
20
- // To enable benchmark, change below line to #if 1
21
- #if 1
22
-
23
20
namespace {
24
21
22
+ #if defined(_MSC_VER)
23
+ #define NON_INLINE __declspec (noinline)
24
+ #else
25
+ // gcc
26
+ #define NON_INLINE __attribute__ ((noinline))
27
+ #endif
28
+
29
+ volatile int globalValue = 0 ;
30
+
31
+ void globalFunction (int a, const int b)
32
+ {
33
+ globalValue += a + b;
34
+ }
35
+
36
+ NON_INLINE void nonInlineGlobalFunction (int a, const int b)
37
+ {
38
+ globalValue += a + b;
39
+ }
40
+
41
+ struct FunctionObject
42
+ {
43
+ void operator () (int a, const int b)
44
+ {
45
+ globalValue += a + b;
46
+ }
47
+
48
+ virtual void virFunc (int a, const int b)
49
+ {
50
+ globalValue += a + b;
51
+ }
52
+
53
+ void nonVirFunc (int a, const int b)
54
+ {
55
+ globalValue += a + b;
56
+ }
57
+
58
+ NON_INLINE virtual void nonInlineVirFunc (int a, const int b)
59
+ {
60
+ globalValue += a + b;
61
+ }
62
+
63
+ NON_INLINE void nonInlineNonVirFunc (int a, const int b)
64
+ {
65
+ globalValue += a + b;
66
+ }
67
+ };
68
+ #undef NON_INLINE
69
+
25
70
template <typename Policies>
26
- void doCallbackListVsFunctionList (const std::string & message)
71
+ using CLT = eventpp::CallbackList<void (int , int ), Policies>;
72
+ using FLT = std::vector<std::function<void (int , int )> >;
73
+
74
+ template <typename Policies, typename AddCL, typename AddFL>
75
+ void doCallbackListVsFunctionList (const std::string & message, AddCL && addCl, AddFL && addFL)
27
76
{
28
- eventpp::CallbackList<void (size_t ), Policies> callbackList;
29
- std::vector<std::function<void (size_t )> > functionList;
30
- constexpr size_t callbackCount = 100 ;
31
- constexpr size_t iterateCount = 1000 * 1000 ;
32
- volatile size_t data = 0 ;
77
+ CLT<Policies> callbackList;
78
+ FLT functionList;
79
+ constexpr int callbackCount = 100 ;
80
+ constexpr int iterateCount = 1000 * 1000 ;
33
81
34
- for (size_t i = 0 ; i < callbackCount; ++i) {
35
- callbackList.append ([&data, i](size_t index ) {
36
- data += i + index ;
37
- });
38
- functionList.push_back ([&data, i](size_t index ) {
39
- data += i + index ;
40
- });
82
+ for (int i = 0 ; i < callbackCount; ++i) {
83
+ addCl (callbackList);
84
+ addFL (functionList);
41
85
}
42
86
const uint64_t timeCallbackList = measureElapsedTime (
43
87
[iterateCount, &callbackList]() {
44
- for (size_t iterate = 0 ; iterate < iterateCount; ++iterate) {
45
- callbackList (iterate);
88
+ for (int iterate = 0 ; iterate < iterateCount; ++iterate) {
89
+ callbackList (iterate, iterate );
46
90
}
47
91
}
48
92
);
49
93
const uint64_t timeFunctionList = measureElapsedTime (
50
94
[iterateCount, &functionList]() {
51
- for (size_t iterate = 0 ; iterate < iterateCount; ++iterate) {
95
+ for (int iterate = 0 ; iterate < iterateCount; ++iterate) {
52
96
for (auto & func : functionList) {
53
- func (iterate);
97
+ func (iterate, iterate );
54
98
}
55
99
}
56
100
}
57
101
);
58
- REQUIRE (data >= 0 );
59
102
60
103
std::cout << message << " timeCallbackList " << timeCallbackList << std::endl;
61
104
std::cout << message << " timeFunctionList " << timeFunctionList << std::endl;
@@ -68,13 +111,60 @@ TEST_CASE("b7, CallbackList vs vector of functions")
68
111
struct PoliciesMultiThreading {
69
112
using Threading = eventpp::MultipleThreading;
70
113
};
71
- doCallbackListVsFunctionList<PoliciesMultiThreading>(" Multi thread" );
72
-
73
114
struct PoliciesSingleThreading {
74
115
using Threading = eventpp::SingleThreading;
75
116
};
76
- doCallbackListVsFunctionList<PoliciesSingleThreading>(" Single thread" );
77
- }
117
+
118
+ struct BenchmarkItem {
119
+ std::string message;
120
+ std::function<void (CLT<PoliciesMultiThreading> &)> addClMulti;
121
+ std::function<void (CLT<PoliciesSingleThreading> &)> addClSingle;
122
+ std::function<void (FLT &)> addFl;
123
+ };
124
+ std::vector<BenchmarkItem> itemList {
125
+ {
126
+ " global" ,
127
+ [](CLT<PoliciesMultiThreading> & cl) {
128
+ cl.append (&globalFunction);
129
+ },
130
+ [](CLT<PoliciesSingleThreading> & cl) {
131
+ cl.append (&globalFunction);
132
+ },
133
+ [](FLT & fl) {
134
+ fl.push_back (&globalFunction);
135
+ }
136
+ },
78
137
138
+ {
139
+ " nonInlineGlobalFunction" ,
140
+ [](CLT<PoliciesMultiThreading> & cl) {
141
+ cl.append (&nonInlineGlobalFunction);
142
+ },
143
+ [](CLT<PoliciesSingleThreading> & cl) {
144
+ cl.append (&nonInlineGlobalFunction);
145
+ },
146
+ [](FLT & fl) {
147
+ fl.push_back (&nonInlineGlobalFunction);
148
+ }
149
+ },
79
150
80
- #endif
151
+ {
152
+ " FunctionObject" ,
153
+ [](CLT<PoliciesMultiThreading> & cl) {
154
+ cl.append (FunctionObject ());
155
+ },
156
+ [](CLT<PoliciesSingleThreading> & cl) {
157
+ cl.append (FunctionObject ());
158
+ },
159
+ [](FLT & fl) {
160
+ fl.push_back (FunctionObject ());
161
+ }
162
+ },
163
+
164
+ };
165
+
166
+ for (BenchmarkItem & item : itemList) {
167
+ doCallbackListVsFunctionList<PoliciesMultiThreading>(" Multi thread, " + item.message , item.addClMulti , item.addFl );
168
+ }
169
+
170
+ }
0 commit comments