Debian Codegraph @

benchmarks/enaml_like_benchmark.cpp

raw
1 /*-----------------------------------------------------------------------------
2 | Copyright (c) 2020-2024, Nucleic Development Team.
3 |
4 | Distributed under the terms of the Modified BSD License.
5 |
6 | The full license is in the file LICENSE, distributed with this software.
7 |----------------------------------------------------------------------------*/
8
9 // Time updating an EditVariable in a set of constraints typical of enaml use.
10
11 #include <kiwi/kiwi.h>
12 #define ANKERL_NANOBENCH_IMPLEMENT
13 #include "nanobench.h"
14
15 using namespace kiwi;
16
17 void build_solver(Solver& solver, Variable& width, Variable& height)
18 {
19 // Create custom strength
20 double mmedium = strength::create(0.0, 1.0, 0.0, 1.25);
21 double smedium = strength::create(0.0, 100, 0.0);
22
23 // Create the variable
24 Variable left("left");
25 Variable top("top");
26 Variable contents_top("contents_top");
27 Variable contents_bottom("contents_bottom");
28 Variable contents_left("contents_left");
29 Variable contents_right("contents_right");
30 Variable midline("midline");
31 Variable ctleft("ctleft");
32 Variable ctheight("ctheight");
33 Variable cttop("cttop");
34 Variable ctwidth("ctwidth");
35 Variable lb1left("lb1left");
36 Variable lb1height("lb1height");
37 Variable lb1top("lb1top");
38 Variable lb1width("lb1width");
39 Variable lb2left("lb2left");
40 Variable lb2height("lb2height");
41 Variable lb2top("lb2top");
42 Variable lb2width("lb2width");
43 Variable lb3left("lb3left");
44 Variable lb3height("lb3height");
45 Variable lb3top("lb3top");
46 Variable lb3width("lb3width");
47 Variable fl1left("fl1left");
48 Variable fl1height("fl1height");
49 Variable fl1top("fl1top");
50 Variable fl1width("fl1width");
51 Variable fl2left("fl2left");
52 Variable fl2height("fl2height");
53 Variable fl2top("fl2top");
54 Variable fl2width("fl2width");
55 Variable fl3left("fl3left");
56 Variable fl3height("fl3height");
57 Variable fl3top("fl3top");
58 Variable fl3width("fl3width");
59
60 // Add the edit variables
61 solver.addEditVariable(width, strength::strong);
62 solver.addEditVariable(height, strength::strong);
63
64 // Add the constraints
65 Constraint constraints[] = {
66 (left + -0 >= 0) | strength::required,
67 (height + 0 == 0) | strength::medium,
68 (top + -0 >= 0) | strength::required,
69 (width + -0 >= 0) | strength::required,
70 (height + -0 >= 0) | strength::required,
71 (-top + contents_top + -10 == 0) | strength::required,
72 (lb3height + -16 == 0) | strength::strong,
73 (lb3height + -16 >= 0) | strength::strong,
74 (ctleft + -0 >= 0) | strength::required,
75 (cttop + -0 >= 0) | strength::required,
76 (ctwidth + -0 >= 0) | strength::required,
77 (ctheight + -0 >= 0) | strength::required,
78 (fl3left + -0 >= 0) | strength::required,
79 (ctheight + -24 >= 0) | smedium,
80 (ctwidth + -1.67772e+07 <= 0) | smedium,
81 (ctheight + -24 <= 0) | smedium,
82 (fl3top + -0 >= 0) | strength::required,
83 (fl3width + -0 >= 0) | strength::required,
84 (fl3height + -0 >= 0) | strength::required,
85 (lb1width + -67 == 0) | strength::weak,
86 (lb2width + -0 >= 0) | strength::required,
87 (lb2height + -0 >= 0) | strength::required,
88 (fl2height + -0 >= 0) | strength::required,
89 (lb3left + -0 >= 0) | strength::required,
90 (fl2width + -125 >= 0) | strength::strong,
91 (fl2height + -21 == 0) | strength::strong,
92 (fl2height + -21 >= 0) | strength::strong,
93 (lb3top + -0 >= 0) | strength::required,
94 (lb3width + -0 >= 0) | strength::required,
95 (fl1left + -0 >= 0) | strength::required,
96 (fl1width + -0 >= 0) | strength::required,
97 (lb1width + -67 >= 0) | strength::strong,
98 (fl2left + -0 >= 0) | strength::required,
99 (lb2width + -66 == 0) | strength::weak,
100 (lb2width + -66 >= 0) | strength::strong,
101 (lb2height + -16 == 0) | strength::strong,
102 (fl1height + -0 >= 0) | strength::required,
103 (fl1top + -0 >= 0) | strength::required,
104 (lb2top + -0 >= 0) | strength::required,
105 (-lb2top + lb3top + -lb2height + -10 == 0) | mmedium,
106 (-lb3top + -lb3height + fl3top + -10 >= 0) | strength::required,
107 (-lb3top + -lb3height + fl3top + -10 == 0) | mmedium,
108 (contents_bottom + -fl3height + -fl3top + -0 == 0) | mmedium,
109 (fl1top + -contents_top + 0 >= 0) | strength::required,
110 (fl1top + -contents_top + 0 == 0) | mmedium,
111 (contents_bottom + -fl3height + -fl3top + -0 >= 0) | strength::required,
112 (-left + -width + contents_right + 10 == 0) | strength::required,
113 (-top + -height + contents_bottom + 10 == 0) | strength::required,
114 (-left + contents_left + -10 == 0) | strength::required,
115 (lb3left + -contents_left + 0 == 0) | mmedium,
116 (fl1left + -midline + 0 == 0) | strength::strong,
117 (fl2left + -midline + 0 == 0) | strength::strong,
118 (ctleft + -midline + 0 == 0) | strength::strong,
119 (fl1top + 0.5 * fl1height + -lb1top + -0.5 * lb1height + 0 == 0) | strength::strong,
120 (lb1left + -contents_left + 0 >= 0) | strength::required,
121 (lb1left + -contents_left + 0 == 0) | mmedium,
122 (-lb1left + fl1left + -lb1width + -10 >= 0) | strength::required,
123 (-lb1left + fl1left + -lb1width + -10 == 0) | mmedium,
124 (-fl1left + contents_right + -fl1width + -0 >= 0) | strength::required,
125 (width + 0 == 0) | strength::medium,
126 (-fl1top + fl2top + -fl1height + -10 >= 0) | strength::required,
127 (-fl1top + fl2top + -fl1height + -10 == 0) | mmedium,
128 (cttop + -fl2top + -fl2height + -10 >= 0) | strength::required,
129 (-ctheight + -cttop + fl3top + -10 >= 0) | strength::required,
130 (contents_bottom + -fl3height + -fl3top + -0 >= 0) | strength::required,
131 (cttop + -fl2top + -fl2height + -10 == 0) | mmedium,
132 (-fl1left + contents_right + -fl1width + -0 == 0) | mmedium,
133 (-lb2top + -0.5 * lb2height + fl2top + 0.5 * fl2height + 0 == 0) | strength::strong,
134 (-contents_left + lb2left + 0 >= 0) | strength::required,
135 (-contents_left + lb2left + 0 == 0) | mmedium,
136 (fl2left + -lb2width + -lb2left + -10 >= 0) | strength::required,
137 (-ctheight + -cttop + fl3top + -10 == 0) | mmedium,
138 (contents_bottom + -fl3height + -fl3top + -0 == 0) | mmedium,
139 (lb1top + -0 >= 0) | strength::required,
140 (lb1width + -0 >= 0) | strength::required,
141 (lb1height + -0 >= 0) | strength::required,
142 (fl2left + -lb2width + -lb2left + -10 == 0) | mmedium,
143 (-fl2left + -fl2width + contents_right + -0 == 0) | mmedium,
144 (-fl2left + -fl2width + contents_right + -0 >= 0) | strength::required,
145 (lb3left + -contents_left + 0 >= 0) | strength::required,
146 (lb1left + -0 >= 0) | strength::required,
147 (0.5 * ctheight + cttop + -lb3top + -0.5 * lb3height + 0 == 0) | strength::strong,
148 (ctleft + -lb3left + -lb3width + -10 >= 0) | strength::required,
149 (-ctwidth + -ctleft + contents_right + -0 >= 0) | strength::required,
150 (ctleft + -lb3left + -lb3width + -10 == 0) | mmedium,
151 (fl3left + -contents_left + 0 >= 0) | strength::required,
152 (fl3left + -contents_left + 0 == 0) | mmedium,
153 (-ctwidth + -ctleft + contents_right + -0 == 0) | mmedium,
154 (-fl3left + contents_right + -fl3width + -0 == 0) | mmedium,
155 (-contents_top + lb1top + 0 >= 0) | strength::required,
156 (-contents_top + lb1top + 0 == 0) | mmedium,
157 (-fl3left + contents_right + -fl3width + -0 >= 0) | strength::required,
158 (lb2top + -lb1top + -lb1height + -10 >= 0) | strength::required,
159 (-lb2top + lb3top + -lb2height + -10 >= 0) | strength::required,
160 (lb2top + -lb1top + -lb1height + -10 == 0) | mmedium,
161 (fl1height + -21 == 0) | strength::strong,
162 (fl1height + -21 >= 0) | strength::strong,
163 (lb2left + -0 >= 0) | strength::required,
164 (lb2height + -16 >= 0) | strength::strong,
165 (fl2top + -0 >= 0) | strength::required,
166 (fl2width + -0 >= 0) | strength::required,
167 (lb1height + -16 >= 0) | strength::strong,
168 (lb1height + -16 == 0) | strength::strong,
169 (fl3width + -125 >= 0) | strength::strong,
170 (fl3height + -21 == 0) | strength::strong,
171 (fl3height + -21 >= 0) | strength::strong,
172 (lb3height + -0 >= 0) | strength::required,
173 (ctwidth + -119 >= 0) | smedium,
174 (lb3width + -24 == 0) | strength::weak,
175 (lb3width + -24 >= 0) | strength::strong,
176 (fl1width + -125 >= 0) | strength::strong,
177 };
178
179 for (const auto& constraint : constraints)
180 solver.addConstraint(constraint);
181 }
182
183 int main()
184 {
185 ankerl::nanobench::Bench().run("building solver", [&] {
186 Solver solver;
187 Variable width("width");
188 Variable height("height");
189 build_solver(solver, width, height);
190 ankerl::nanobench::doNotOptimizeAway(solver); //< prevent the compiler to optimize away the solver
191 });
192
193 struct Size
194 {
195 int width;
196 int height;
197 };
198
199 Size sizes[] = {
200 { 400, 600 },
201 { 600, 400 },
202 { 800, 1200 },
203 { 1200, 800 },
204 { 400, 800 },
205 { 800, 400 }
206 };
207
208 Solver solver;
209 Variable widthVar("width");
210 Variable heightVar("height");
211 build_solver(solver, widthVar, heightVar);
212
213 for (const Size& size : sizes)
214 {
215 double width = size.width;
216 double height = size.height;
217
218 ankerl::nanobench::Bench().minEpochIterations(10).run("suggest value " + std::to_string(size.width) + "x" + std::to_string(size.height), [&] {
219 solver.suggestValue(widthVar, width);
220 solver.suggestValue(heightVar, height);
221 solver.updateVariables();
222 });
223 }
224 }