1
1
# Micro Benchmarks on Value Object options in Ruby
2
2
3
- A series of micro benchmarks about Data.define vs Struct vs OpenStruct in #Ruby.
4
-
5
3
This is executed with defaults, no extra settings added.
6
4
7
- The tests are focused on using keyword arguments. The results might be different when using positional arguments.
8
-
9
5
## How to run the benchmarks
10
6
11
7
``` bash
12
8
bundle install
13
9
bundle exec ruby < benchmark>
14
10
```
15
11
16
- ## Results on my machine
12
+ ## Machine use to run the benchmarks
17
13
18
14
I run the following benchmarks on my machine:
19
15
@@ -22,6 +18,12 @@ I run the following benchmarks on my machine:
22
18
- Running Mac OS 14.4 (23E214)
23
19
- Ruby 3.3.0
24
20
21
+ ## Comparing Data.define with Struct and OpenStruct
22
+
23
+ Comparing Data.define with Struct and OpenStruct.
24
+
25
+ The benchmark is focused on benchmarking the keyword arguments.
26
+
25
27
### Creating Values
26
28
27
29
#### Benchmark with ` bmbm `
@@ -128,3 +130,130 @@ Comparison:
128
130
Struct: 28419.6 i/s - same-ish: difference falls within error
129
131
OpenStruct: 13474.6 i/s - 2.13x slower
130
132
```
133
+
134
+ ## Comparing Data.define
135
+
136
+ ### Comparing multiple ways to create a new object
137
+
138
+ #### Benchmark with ` bmbm `
139
+
140
+ This benchmark is run with Ruby default benchmark using ` bmbm `
141
+
142
+ ``` bash
143
+ Comparing ways to instantiate a Data.define object - Benchmark with bmbm
144
+ Rehearsal --------------------------------------------------------
145
+ Keyword arguments 0.000025 0.000000 0.000025 ( 0.000025)
146
+ Positional arguments 0.000058 0.000000 0.000058 ( 0.000059)
147
+ Constructor method 0.000053 0.000000 0.000053 ( 0.000054)
148
+ Constructor keywords 0.000029 0.000000 0.000029 ( 0.000029)
149
+ ----------------------------------------------- total: 0.000165sec
150
+
151
+ user system total real
152
+ Keyword arguments 0.000023 0.000001 0.000024 ( 0.000023)
153
+ Positional arguments 0.000039 0.000000 0.000039 ( 0.000039)
154
+ Constructor method 0.000046 0.000000 0.000046 ( 0.000046)
155
+ Constructor keywords 0.000023 0.000000 0.000023 ( 0.000023)
156
+ ```
157
+
158
+ #### Benchmarking with ` ips `
159
+
160
+ This benchmark is run with ` benchmark-ips ` gem.
161
+
162
+ ``` bash
163
+ Comparing ways to instantiate a Data.define object - Benchmark with ips
164
+ ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23]
165
+ Warming up --------------------------------------
166
+ Keyword arguments 5.345k i/100ms
167
+ Positional arguments 2.692k i/100ms
168
+ Constructor method 2.663k i/100ms
169
+ Constructor keywords 5.354k i/100ms
170
+ Calculating -------------------------------------
171
+ Keyword arguments 52.390k (± 1.9%) i/s - 261.905k in 5.000971s
172
+ Positional arguments 26.162k (± 1.1%) i/s - 131.908k in 5.042607s
173
+ Constructor method 26.149k (± 0.8%) i/s - 133.150k in 5.092403s
174
+ Constructor keywords 51.813k (± 1.6%) i/s - 262.346k in 5.064698s
175
+
176
+ Comparison:
177
+ Keyword arguments: 52390.1 i/s
178
+ Constructor keywords: 51812.7 i/s - same-ish: difference falls within error
179
+ Positional arguments: 26162.3 i/s - 2.00x slower
180
+ Constructor method: 26148.6 i/s - 2.00x slower
181
+ ```
182
+
183
+ ### Benchmarking with ` memory `
184
+
185
+ This benchmark is run with ` benchmark-memory ` gem.
186
+
187
+ This test is probably unnecessary cause in the end it creates the same thing.
188
+
189
+ ``` bash
190
+ Comparing ways to instantiate a Data.define object - Benchmark with memory
191
+ Calculating -------------------------------------
192
+ Keyword arguments 36.792k memsize ( 0.000 retained)
193
+ 2.000 objects ( 0.000 retained)
194
+ 0.000 strings ( 0.000 retained)
195
+ Positional arguments 36.792k memsize ( 0.000 retained)
196
+ 2.000 objects ( 0.000 retained)
197
+ 0.000 strings ( 0.000 retained)
198
+ Constructor method 36.792k memsize ( 0.000 retained)
199
+ 2.000 objects ( 0.000 retained)
200
+ 0.000 strings ( 0.000 retained)
201
+ Constructor keywords 36.792k memsize ( 0.000 retained)
202
+ 2.000 objects ( 0.000 retained)
203
+ 0.000 strings ( 0.000 retained)
204
+
205
+ Comparison:
206
+ Keyword arguments: 36792 allocated
207
+ Positional arguments: 36792 allocated - same
208
+ Constructor method: 36792 allocated - same
209
+ Constructor keywords: 36792 allocated - same
210
+ ```
211
+
212
+ ### Comparing accessing data from objects created differently
213
+
214
+ There is no difference in accessing attributes depending on how the object was created.
215
+
216
+ #### Benchmark with ` bmbm `
217
+
218
+ This benchmark is run with Ruby default benchmark using ` bmbm `
219
+
220
+ ``` bash
221
+ Comparing accessing data for Data.define object - Benchmark with bmbm
222
+ Rehearsal --------------------------------------------------------
223
+ Keyword arguments 0.000072 0.000002 0.000074 ( 0.000074)
224
+ Positional arguments 0.000046 0.000001 0.000047 ( 0.000047)
225
+ Constructor method 0.000047 0.000000 0.000047 ( 0.000047)
226
+ Constructor keywords 0.000047 0.000001 0.000048 ( 0.000048)
227
+ ----------------------------------------------- total: 0.000216sec
228
+
229
+ user system total real
230
+ Keyword arguments 0.000048 0.000001 0.000049 ( 0.000048)
231
+ Positional arguments 0.000047 0.000000 0.000047 ( 0.000047)
232
+ Constructor method 0.000047 0.000000 0.000047 ( 0.000048)
233
+ Constructor keywords 0.000047 0.000001 0.000048 ( 0.000047)
234
+ ```
235
+
236
+ #### Benchmark with ` ips `
237
+
238
+ This benchmark is run with ` benchmark-ips ` gem.
239
+
240
+ ``` bash
241
+ Comparing accessing data for Data.define object - Benchmark with bmbm
242
+ ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23]
243
+ Warming up --------------------------------------
244
+ Keyword arguments 2.669k i/100ms
245
+ Positional arguments 2.657k i/100ms
246
+ Constructor method 2.685k i/100ms
247
+ Constructor keywords 2.690k i/100ms
248
+ Calculating -------------------------------------
249
+ Keyword arguments 26.925k (± 0.5%) i/s - 136.119k in 5.055637s
250
+ Positional arguments 26.835k (± 0.4%) i/s - 135.507k in 5.049741s
251
+ Constructor method 26.895k (± 0.3%) i/s - 136.935k in 5.091470s
252
+ Constructor keywords 26.794k (± 0.4%) i/s - 134.500k in 5.019767s
253
+
254
+ Comparison:
255
+ Keyword arguments: 26924.8 i/s
256
+ Constructor method: 26895.3 i/s - same-ish: difference falls within error
257
+ Positional arguments: 26834.9 i/s - same-ish: difference falls within error
258
+ Constructor keywords: 26794.4 i/s - same-ish: difference falls within error
259
+ ```
0 commit comments