|
| 1 | +require 'benchmark' |
| 2 | + |
| 3 | +class SequentialWidget |
| 4 | + def initialize |
| 5 | + a = 1 |
| 6 | + b = 2 |
| 7 | + end |
| 8 | +end |
| 9 | + |
| 10 | +def sequential_initialize_assignment |
| 11 | + SequentialWidget.new |
| 12 | +end |
| 13 | + |
| 14 | +class ParallelWidget |
| 15 | + def initialize |
| 16 | + a, b = 1, 2 |
| 17 | + end |
| 18 | +end |
| 19 | + |
| 20 | +def parallel_initialize_assignment |
| 21 | + ParallelWidget.new |
| 22 | +end |
| 23 | + |
| 24 | +def sequential_local_assignment |
| 25 | + a = 1 |
| 26 | + b = 2 |
| 27 | + # Simulate that the assignment is not the method return value but do |
| 28 | + # not add more work to this method |
| 29 | + :noop |
| 30 | +end |
| 31 | + |
| 32 | +def parallel_local_assignment |
| 33 | + a, b = 1, 2 |
| 34 | + # Simulate that the assignment is not the method return value but do |
| 35 | + # not add more work to this method |
| 36 | + :noop |
| 37 | +end |
| 38 | + |
| 39 | +def sequential_block_assignment |
| 40 | + a = nil |
| 41 | + b = nil |
| 42 | + (1..10).each do |x| |
| 43 | + a = b |
| 44 | + b = x |
| 45 | + end |
| 46 | + # Simulate that the assignment is not the method return value but do |
| 47 | + # not add more work to this method |
| 48 | + :noop |
| 49 | +end |
| 50 | + |
| 51 | +def parallel_block_assignment |
| 52 | + a = nil |
| 53 | + b = nil |
| 54 | + (1..10).each do |x| |
| 55 | + a, b = b, x |
| 56 | + end |
| 57 | + # Simulate that the assignment is not the method return value but do |
| 58 | + # not add more work to this method |
| 59 | + :noop |
| 60 | +end |
| 61 | + |
| 62 | + |
| 63 | +puts |
| 64 | +puts "Ruby #{RUBY_VERSION}" |
| 65 | +puts |
| 66 | +begin |
| 67 | + require 'benchmark/ips' |
| 68 | + |
| 69 | + Benchmark.ips do |x| |
| 70 | + x.report('Parallel Local Assignment ') do |
| 71 | + parallel_local_assignment |
| 72 | + end |
| 73 | + |
| 74 | + x.report('Sequential Local Assignment ') do |
| 75 | + sequential_local_assignment |
| 76 | + end |
| 77 | + |
| 78 | + x.compare! |
| 79 | + end |
| 80 | + |
| 81 | + Benchmark.ips do |x| |
| 82 | + x.report('Parallel Initialize Assignment ') do |
| 83 | + parallel_initialize_assignment |
| 84 | + end |
| 85 | + |
| 86 | + x.report('Sequential Initialize Assignment') do |
| 87 | + sequential_initialize_assignment |
| 88 | + end |
| 89 | + |
| 90 | + x.compare! |
| 91 | + end |
| 92 | + |
| 93 | + Benchmark.ips do |x| |
| 94 | + x.report('Parallel Block Assignment ') do |
| 95 | + parallel_block_assignment |
| 96 | + end |
| 97 | + |
| 98 | + x.report('Sequential Block Assignment ') do |
| 99 | + sequential_block_assignment |
| 100 | + end |
| 101 | + |
| 102 | + x.compare! |
| 103 | + end |
| 104 | + |
| 105 | +rescue LoadError |
| 106 | + |
| 107 | + # We are on an older Ruby which does not support benchmark/ips |
| 108 | + n = 100_000 |
| 109 | + |
| 110 | + puts "#{n} times" |
| 111 | + |
| 112 | + Benchmark.benchmark do |bm| |
| 113 | + puts |
| 114 | + puts "Parallel Local Assignment" |
| 115 | + 3.times { bm.report { n.times { parallel_local_assignment } } } |
| 116 | + |
| 117 | + puts |
| 118 | + puts "Sequential Local Assignment" |
| 119 | + 3.times { bm.report { n.times { sequential_local_assignment } } } |
| 120 | + end |
| 121 | + |
| 122 | + Benchmark.benchmark do |bm| |
| 123 | + puts |
| 124 | + puts "Parallel Initialize Assignment" |
| 125 | + 3.times { bm.report { n.times { parallel_initialize_assignment } } } |
| 126 | + |
| 127 | + puts |
| 128 | + puts "Sequential Initialize Assignment" |
| 129 | + 3.times { bm.report { n.times { sequential_initialize_assignment } } } |
| 130 | + end |
| 131 | + |
| 132 | + Benchmark.benchmark do |bm| |
| 133 | + puts |
| 134 | + puts "Parallel Block Assignment" |
| 135 | + 3.times { bm.report { n.times { parallel_block_assignment } } } |
| 136 | + |
| 137 | + puts |
| 138 | + puts "Sequential Block Assignment" |
| 139 | + 3.times { bm.report { n.times { sequential_block_assignment } } } |
| 140 | + end |
| 141 | +end |
| 142 | + |
| 143 | +__END__ |
| 144 | + |
| 145 | +This is a complicated benchmark. Sequential assignment is always faster on Ruby |
| 146 | +1.8.7, REE, and JRuby. However, on Ruby 1.9 and 2.x it depends where the |
| 147 | +assignment is used - and how many assignments are made. |
| 148 | + |
| 149 | +On Ruby 1.9 and 2.x the assignment of two variable is essentially equally fast |
| 150 | +for parallel and sequent. However, as the number of assignments go up parallel |
| 151 | +assignment becomes increasingly faster. However, it depends on the context of |
| 152 | +that assignment. If the assignment LOC is also an _**implicit**_ return, thus |
| 153 | +if it's the last line of a method or a block (this is true for blocks even when |
| 154 | +the block return value is ignored), then Ruby will return all variable values |
| 155 | +as an array! This array creation is slow and will make sequential assignment |
| 156 | +faster, as the incidental array is not created. |
| 157 | + |
| 158 | +Given that there is little benefit for the common case of parallel assignment |
| 159 | +for two varaibles, and it's less common to see parallel assignment for three |
| 160 | +variables, and extremely rare for any number greater we should default to |
| 161 | +sequential assignment. [Aaron Kromer] |
| 162 | + |
| 163 | +Ruby 1.8.7 |
| 164 | + |
| 165 | +100000 times |
| 166 | + |
| 167 | +Parallel Local Assignment |
| 168 | + 0.080000 0.000000 0.080000 ( 0.079573) |
| 169 | + 0.060000 0.000000 0.060000 ( 0.067334) |
| 170 | + 0.060000 0.000000 0.060000 ( 0.059873) |
| 171 | + |
| 172 | +Sequential Local Assignment |
| 173 | + 0.030000 0.000000 0.030000 ( 0.030429) |
| 174 | + 0.030000 0.010000 0.040000 ( 0.030158) |
| 175 | + 0.020000 0.000000 0.020000 ( 0.023209) |
| 176 | + |
| 177 | +Parallel Initialize Assignment |
| 178 | + 0.090000 0.000000 0.090000 ( 0.085544) |
| 179 | + 0.080000 0.000000 0.080000 ( 0.084318) |
| 180 | + 0.080000 0.000000 0.080000 ( 0.085762) |
| 181 | + |
| 182 | +Sequential Initialize Assignment |
| 183 | + 0.050000 0.000000 0.050000 ( 0.048160) |
| 184 | + 0.050000 0.000000 0.050000 ( 0.049463) |
| 185 | + 0.050000 0.000000 0.050000 ( 0.051358) |
| 186 | + |
| 187 | +Parallel Block Assignment |
| 188 | + 0.460000 0.000000 0.460000 ( 0.469698) |
| 189 | + 0.460000 0.000000 0.460000 ( 0.457632) |
| 190 | + 0.450000 0.010000 0.460000 ( 0.462481) |
| 191 | + |
| 192 | +Sequential Block Assignment |
| 193 | + 0.230000 0.000000 0.230000 ( 0.240600) |
| 194 | + 0.230000 0.000000 0.230000 ( 0.245943) |
| 195 | + 0.230000 0.000000 0.230000 ( 0.230579) |
| 196 | + |
| 197 | + |
| 198 | +Ruby 1.9.3 |
| 199 | + |
| 200 | +Calculating ------------------------------------- |
| 201 | +Parallel Local Assignment |
| 202 | + 89.220k i/100ms |
| 203 | +Sequential Local Assignment |
| 204 | + 91.383k i/100ms |
| 205 | +------------------------------------------------- |
| 206 | +Parallel Local Assignment |
| 207 | + 5.075M (± 8.8%) i/s - 25.249M |
| 208 | +Sequential Local Assignment |
| 209 | + 5.029M (±12.1%) i/s - 24.673M |
| 210 | + |
| 211 | +Comparison: |
| 212 | +Parallel Local Assignment : 5074905.9 i/s |
| 213 | +Sequential Local Assignment : 5028923.8 i/s - 1.01x slower |
| 214 | + |
| 215 | +Calculating ------------------------------------- |
| 216 | +Parallel Initialize Assignment |
| 217 | + 61.930k i/100ms |
| 218 | +Sequential Initialize Assignment |
| 219 | + 66.286k i/100ms |
| 220 | +------------------------------------------------- |
| 221 | +Parallel Initialize Assignment |
| 222 | + 1.943M (± 5.3%) i/s - 9.723M |
| 223 | +Sequential Initialize Assignment |
| 224 | + 2.124M (± 9.8%) i/s - 10.606M |
| 225 | + |
| 226 | +Comparison: |
| 227 | +Sequential Initialize Assignment: 2123826.3 i/s |
| 228 | +Parallel Initialize Assignment : 1943162.0 i/s - 1.09x slower |
| 229 | + |
| 230 | +Calculating ------------------------------------- |
| 231 | +Parallel Block Assignment |
| 232 | + 30.497k i/100ms |
| 233 | +Sequential Block Assignment |
| 234 | + 36.997k i/100ms |
| 235 | +------------------------------------------------- |
| 236 | +Parallel Block Assignment |
| 237 | + 579.905k (± 7.7%) i/s - 2.897M |
| 238 | +Sequential Block Assignment |
| 239 | + 896.073k (± 7.7%) i/s - 4.477M |
| 240 | + |
| 241 | +Comparison: |
| 242 | +Sequential Block Assignment : 896072.9 i/s |
| 243 | +Parallel Block Assignment : 579904.9 i/s - 1.55x slower |
| 244 | + |
| 245 | + |
| 246 | +Ruby 2.2.2 |
| 247 | + |
| 248 | +Calculating ------------------------------------- |
| 249 | +Parallel Local Assignment |
| 250 | + 83.984k i/100ms |
| 251 | +Sequential Local Assignment |
| 252 | + 86.309k i/100ms |
| 253 | +------------------------------------------------- |
| 254 | +Parallel Local Assignment |
| 255 | + 5.602M (±12.0%) i/s - 27.043M |
| 256 | +Sequential Local Assignment |
| 257 | + 5.482M (± 8.0%) i/s - 27.274M |
| 258 | + |
| 259 | +Comparison: |
| 260 | +Parallel Local Assignment : 5602180.9 i/s |
| 261 | +Sequential Local Assignment : 5482370.9 i/s - 1.02x slower |
| 262 | + |
| 263 | +Calculating ------------------------------------- |
| 264 | +Parallel Initialize Assignment |
| 265 | + 64.000k i/100ms |
| 266 | +Sequential Initialize Assignment |
| 267 | + 67.066k i/100ms |
| 268 | +------------------------------------------------- |
| 269 | +Parallel Initialize Assignment |
| 270 | + 2.044M (± 5.5%) i/s - 10.240M |
| 271 | +Sequential Initialize Assignment |
| 272 | + 2.488M (± 6.3%) i/s - 12.407M |
| 273 | + |
| 274 | +Comparison: |
| 275 | +Sequential Initialize Assignment: 2487575.1 i/s |
| 276 | +Parallel Initialize Assignment : 2044428.9 i/s - 1.22x slower |
| 277 | + |
| 278 | +Calculating ------------------------------------- |
| 279 | +Parallel Block Assignment |
| 280 | + 34.362k i/100ms |
| 281 | +Sequential Block Assignment |
| 282 | + 45.586k i/100ms |
| 283 | +------------------------------------------------- |
| 284 | +Parallel Block Assignment |
| 285 | + 593.794k (± 4.5%) i/s - 2.989M |
| 286 | +Sequential Block Assignment |
| 287 | + 886.536k (±12.3%) i/s - 4.331M |
| 288 | + |
| 289 | +Comparison: |
| 290 | +Sequential Block Assignment : 886535.9 i/s |
| 291 | +Parallel Block Assignment : 593793.6 i/s - 1.49x slower |
0 commit comments