diff --git a/bench.rb b/bench.rb
index e05833b..2c47c62 100644
|
a
|
b
|
|
| 7 | 7 | # The same script can also be executed using /usr/bin/ruby or ruby19 to |
| 8 | 8 | # compare the implementations. |
| 9 | 9 | |
| | 10 | def executeWithErrorHandling |
| | 11 | if (block_given?) |
| | 12 | begin |
| | 13 | yield |
| | 14 | rescue Exception => e |
| | 15 | print(" ************* BENCHMARK FAILED ************* ") |
| | 16 | end |
| | 17 | end |
| | 18 | end |
| | 19 | |
| 10 | 20 | def fib(n) |
| 11 | 21 | if n < 3 |
| 12 | 22 | 1 |
| … |
… |
|
| 80 | 90 | |
| 81 | 91 | # Fixnum arithmetic. |
| 82 | 92 | bm.report('10 fib(30)') do |
| 83 | | i=0; while i<10; fib(30); i+=1; end |
| | 93 | executeWithErrorHandling do |
| | 94 | i=0; while i<10; fib(30); i+=1; end |
| | 95 | end |
| 84 | 96 | end |
| 85 | 97 | bm.report('10 fib(35)') do |
| 86 | | i=0; while i<10; fib(35); i+=1; end |
| | 98 | executeWithErrorHandling do |
| | 99 | i=0; while i<10; fib(35); i+=1; end |
| | 100 | end |
| | 101 | end |
| | 102 | bm.report('tak') do |
| | 103 | executeWithErrorHandling do |
| | 104 | tak(18,9,0) |
| | 105 | end |
| | 106 | end |
| | 107 | bm.report('tarai') do |
| | 108 | executeWithErrorHandling do |
| | 109 | tarai(12,6,0) |
| | 110 | end |
| 87 | 111 | end |
| 88 | | bm.report('tak') { tak(18,9,0) } |
| 89 | | bm.report('tarai') { tarai(12,6,0) } |
| 90 | | if RUBY_VERSION.to_f > 1.8 |
| 91 | | # Ruby 1.8 is too weak for this benchmark. |
| 92 | | bm.report('ackermann') { ack(3,9) } |
| | 112 | bm.report('ackermann') do |
| | 113 | executeWithErrorHandling do |
| | 114 | ack(3,9) |
| | 115 | end |
| 93 | 116 | end |
| 94 | 117 | |
| 95 | 118 | # Loops. |
| 96 | 119 | bm.report('10000000 times loop') do |
| 97 | | 3000000.times {} |
| | 120 | executeWithErrorHandling do |
| | 121 | 10000000.times {} |
| | 122 | end |
| 98 | 123 | end |
| 99 | 124 | bm.report('30000000 times loop') do |
| 100 | | 30000000.times {} |
| | 125 | executeWithErrorHandling do |
| | 126 | 30000000.times {} |
| | 127 | end |
| 101 | 128 | end |
| 102 | 129 | bm.report('10000000 while loop') do |
| 103 | | i=0; while i<10000000; i+=1; end |
| | 130 | executeWithErrorHandling do |
| | 131 | i=0; while i<10000000; i+=1; end |
| | 132 | end |
| 104 | 133 | end |
| 105 | 134 | bm.report('60000000 while loop') do |
| 106 | | i=0; while i<60000000; i+=1; end |
| | 135 | executeWithErrorHandling do |
| | 136 | i=0; while i<60000000; i+=1; end |
| | 137 | end |
| 107 | 138 | end |
| 108 | 139 | |
| 109 | 140 | # Messages. |
| 110 | 141 | bm.report('30000000 msg w/ 0 arg') do |
| 111 | | o = Class1.new |
| 112 | | i=0; while i<10000000; o.method1; o.method1; o.method1; i+=1; end |
| | 142 | executeWithErrorHandling do |
| | 143 | o = Class1.new |
| | 144 | i=0; while i<30000000; o.method1; o.method1; o.method1; i+=1; end |
| | 145 | end |
| 113 | 146 | end |
| 114 | 147 | bm.report('30000000 msg w/ 1 arg') do |
| 115 | | o = Class1.new |
| 116 | | i=0; while i<10000000; o.method2(i); o.method2(i); o.method2(i); i+=1; end |
| | 148 | executeWithErrorHandling do |
| | 149 | o = Class1.new |
| | 150 | i=0; while i<30000000; o.method2(i); o.method2(i); o.method2(i); i+=1; end |
| | 151 | end |
| 117 | 152 | end |
| 118 | 153 | bm.report('10000000 super w/ 0 arg') do |
| 119 | | o = Class2.new |
| 120 | | i=0; while i<10000000; o.method1; i+=1; end |
| | 154 | executeWithErrorHandling do |
| | 155 | o = Class2.new |
| | 156 | i=0; while i<10000000; o.method1; i+=1; end |
| | 157 | end |
| 121 | 158 | end |
| 122 | 159 | bm.report('10000000 super w/ 1 arg') do |
| 123 | | o = Class2.new |
| 124 | | i=0; while i<10000000; o.method2(i); i+=1; end |
| | 160 | executeWithErrorHandling do |
| | 161 | o = Class2.new |
| | 162 | i=0; while i<10000000; o.method2(i); i+=1; end |
| | 163 | end |
| 125 | 164 | end |
| 126 | 165 | bm.report('10000000 #send') do |
| 127 | | o = Class1.new |
| 128 | | i=0; while i<10000000; o.send(:method1); i+=1; end |
| | 166 | executeWithErrorHandling do |
| | 167 | o = Class1.new |
| | 168 | i=0; while i<10000000; o.send(:method1); i+=1; end |
| | 169 | end |
| 129 | 170 | end |
| 130 | 171 | bm.report('30000000 tail calls') do |
| 131 | | tail(30000000) |
| | 172 | executeWithErrorHandling do |
| | 173 | tail(30000000) |
| | 174 | end |
| 132 | 175 | end |
| 133 | 176 | |
| 134 | 177 | # Instance variables. |
| 135 | | bm.report('10000000 ivar read') { bench_ivar_read(10000000) } |
| 136 | | bm.report('30000000 ivar read') { bench_ivar_read(30000000) } |
| 137 | | bm.report('10000000 ivar write') { bench_ivar_write(10000000) } |
| 138 | | bm.report('30000000 ivar write') { bench_ivar_write(30000000) } |
| | 178 | bm.report('10000000 ivar read') do |
| | 179 | executeWithErrorHandling do |
| | 180 | bench_ivar_read(10000000) |
| | 181 | end |
| | 182 | end |
| | 183 | bm.report('30000000 ivar read') do |
| | 184 | executeWithErrorHandling do |
| | 185 | bench_ivar_read(30000000) |
| | 186 | end |
| | 187 | end |
| | 188 | bm.report('10000000 ivar write') do |
| | 189 | executeWithErrorHandling do |
| | 190 | bench_ivar_write(10000000) |
| | 191 | end |
| | 192 | end |
| | 193 | bm.report('30000000 ivar write') do |
| | 194 | executeWithErrorHandling do |
| | 195 | bench_ivar_write(30000000) |
| | 196 | end |
| | 197 | end |
| 139 | 198 | |
| 140 | 199 | # Const lookup. |
| 141 | 200 | bm.report('30000000 const') do |
| 142 | | i=0; while i<30000000; i+=ConstantOne; end |
| | 201 | executeWithErrorHandling do |
| | 202 | i=0; while i<30000000; i+=ConstantOne; end |
| | 203 | end |
| 143 | 204 | end |
| 144 | 205 | |
| 145 | 206 | # Blocks |
| 146 | 207 | bm.report('30000000 yield') do |
| 147 | | o = Class1.new |
| 148 | | o.yield1(30000000) {} |
| | 208 | executeWithErrorHandling do |
| | 209 | o = Class1.new |
| | 210 | o.yield1(30000000) {} |
| | 211 | end |
| 149 | 212 | end |
| 150 | 213 | bm.report('30000000 msg w/ block+yield') do |
| 151 | | o = Class1.new |
| 152 | | i=0; while i<30000000; o.yield2 {}; i+=1; end |
| | 214 | executeWithErrorHandling do |
| | 215 | o = Class1.new |
| | 216 | i=0; while i<30000000; o.yield2 {}; i+=1; end |
| | 217 | end |
| 153 | 218 | end |
| 154 | 219 | bm.report('30000000 Proc#call') do |
| 155 | | o = proc {} |
| 156 | | i=0; while i<30000000; o.call; i+=1; end |
| | 220 | executeWithErrorHandling do |
| | 221 | o = proc {} |
| | 222 | i=0; while i<30000000; o.call; i+=1; end |
| | 223 | end |
| 157 | 224 | end |
| 158 | 225 | bm.report('30000000 dvar write') do |
| 159 | | i=0 |
| 160 | | 30000000.times { i=1 } |
| | 226 | executeWithErrorHandling do |
| | 227 | i=0 |
| | 228 | 30000000.times { i=1 } |
| | 229 | end |
| 161 | 230 | end |
| 162 | 231 | |
| 163 | 232 | # Eval |
| 164 | 233 | bm.report('1000 eval') do |
| 165 | | i=0 |
| 166 | | s = "#{1+1}+#{20+20}" |
| 167 | | while i<1000 |
| 168 | | eval(s) |
| 169 | | i+=1 |
| | 234 | executeWithErrorHandling do |
| | 235 | i=0 |
| | 236 | s = "#{1+1}+#{20+20}" |
| | 237 | while i<1000 |
| | 238 | eval(s) |
| | 239 | i+=1 |
| | 240 | end |
| 170 | 241 | end |
| 171 | 242 | end |
| 172 | 243 | bm.report('30000000 binding-var write') do |
| 173 | | i=0 |
| 174 | | eval('while i<30000000; i+=1; end') |
| | 244 | executeWithErrorHandling do |
| | 245 | i=0 |
| | 246 | eval('while i<30000000; i+=1; end') |
| | 247 | end |
| 175 | 248 | end |
| 176 | 249 | |
| 177 | 250 | # Break |
| 178 | 251 | bm.report('30000000 while break') do |
| 179 | | i=0; while i<30000000; while true; i+=1; break; end; end |
| | 252 | executeWithErrorHandling do |
| | 253 | i=0; while i<30000000; while true; i+=1; break; end; end |
| | 254 | end |
| 180 | 255 | end |
| 181 | 256 | bm.report('10000000 block break') do |
| 182 | | i=0; while i<10000000; 1.times { i+=1; break }; end |
| | 257 | executeWithErrorHandling do |
| | 258 | i=0; while i<10000000; 1.times { i+=1; break }; end |
| | 259 | end |
| 183 | 260 | end |
| 184 | 261 | |
| 185 | 262 | # Next |
| 186 | 263 | bm.report('30000000 while next') do |
| 187 | | i=0; while i<30000000; i+=1; next; exit; end |
| | 264 | executeWithErrorHandling do |
| | 265 | i=0; while i<30000000; i+=1; next; exit; end |
| | 266 | end |
| 188 | 267 | end |
| 189 | 268 | bm.report('10000000 block next') do |
| 190 | | i=0; while i<10000000; 1.times { i+=1; next; exit; }; end |
| | 269 | executeWithErrorHandling do |
| | 270 | i=0; while i<10000000; 1.times { i+=1; next; exit; }; end |
| | 271 | end |
| 191 | 272 | end |
| 192 | 273 | |
| 193 | 274 | # Exception handlers. |
| 194 | 275 | bm.report('60000000 begin w/o exception') do |
| 195 | | i=0 |
| 196 | | while i<60000000 |
| 197 | | begin |
| 198 | | i+=1 |
| 199 | | rescue |
| | 276 | executeWithErrorHandling do |
| | 277 | i=0 |
| | 278 | while i<60000000 |
| | 279 | begin |
| | 280 | i+=1 |
| | 281 | rescue |
| | 282 | end |
| 200 | 283 | end |
| 201 | 284 | end |
| 202 | 285 | end |
| 203 | 286 | bm.report('60000000 ensure w/o exception') do |
| 204 | | i=0 |
| 205 | | while i<60000000 |
| 206 | | begin |
| | 287 | executeWithErrorHandling do |
| | 288 | i=0 |
| | 289 | while i<60000000 |
| 207 | 290 | begin |
| | 291 | begin |
| | 292 | ensure |
| | 293 | i+=1 |
| | 294 | end |
| 208 | 295 | ensure |
| 209 | 296 | i+=1 |
| 210 | 297 | end |
| 211 | | ensure |
| 212 | | i+=1 |
| 213 | 298 | end |
| 214 | 299 | end |
| 215 | 300 | end |
| 216 | 301 | bm.report('50000 raise') do |
| 217 | | i=0 |
| 218 | | while i<50000 |
| 219 | | begin |
| 220 | | raise |
| 221 | | rescue |
| 222 | | i+=1 |
| | 302 | executeWithErrorHandling do |
| | 303 | i=0 |
| | 304 | while i<50000 |
| | 305 | begin |
| | 306 | raise |
| | 307 | rescue |
| | 308 | i+=1 |
| | 309 | end |
| 223 | 310 | end |
| 224 | 311 | end |
| 225 | 312 | end |
| 226 | 313 | |
| 227 | 314 | # Method |
| 228 | 315 | bm.report('3000000 Method#call w/ 0 arg') do |
| 229 | | o = Class1.new |
| 230 | | m = o.method(:method1) |
| 231 | | i=0 |
| 232 | | while i<3000000 |
| 233 | | m.call |
| 234 | | i+=1 |
| | 316 | executeWithErrorHandling do |
| | 317 | o = Class1.new |
| | 318 | m = o.method(:method1) |
| | 319 | i=0 |
| | 320 | while i<3000000 |
| | 321 | m.call |
| | 322 | i+=1 |
| | 323 | end |
| 235 | 324 | end |
| 236 | 325 | end |
| 237 | 326 | bm.report('3000000 Method#call w/ 1 arg') do |
| 238 | | o = Class1.new |
| 239 | | m = o.method(:method2) |
| 240 | | i=0 |
| 241 | | while i<3000000 |
| 242 | | m.call(i) |
| 243 | | i+=1 |
| | 327 | executeWithErrorHandling do |
| | 328 | o = Class1.new |
| | 329 | m = o.method(:method2) |
| | 330 | i=0 |
| | 331 | while i<3000000 |
| | 332 | m.call(i) |
| | 333 | i+=1 |
| | 334 | end |
| 244 | 335 | end |
| 245 | 336 | end |
| 246 | | |
| 247 | 337 | end |