|
5 | 5 | # Modified by Wesley Moxam |
6 | 6 | # *reset* |
7 | 7 |
|
8 | | -def item_check(left, right) |
9 | | - return 1 if left.nil? |
10 | | - 1 + item_check(*left) + item_check(*right) |
11 | | -end |
| 8 | +module Btree |
| 9 | + extend self |
12 | 10 |
|
13 | | -def bottom_up_tree(depth) |
14 | | - return [nil, nil] unless depth > 0 |
15 | | - depth -= 1 |
16 | | - [bottom_up_tree(depth), bottom_up_tree(depth)] |
17 | | -end |
| 11 | + def item_check(left, right) |
| 12 | + return 1 if left.nil? |
| 13 | + 1 + item_check(*left) + item_check(*right) |
| 14 | + end |
18 | 15 |
|
19 | | -max_depth = 14 |
20 | | -min_depth = 4 |
| 16 | + def bottom_up_tree(depth) |
| 17 | + return [nil, nil] unless depth > 0 |
| 18 | + depth -= 1 |
| 19 | + [bottom_up_tree(depth), bottom_up_tree(depth)] |
| 20 | + end |
21 | 21 |
|
22 | | -max_depth = min_depth + 2 if min_depth + 2 > max_depth |
23 | | -stretch_depth = max_depth + 1 |
| 22 | + def bench |
| 23 | + max_depth = 14 |
| 24 | + min_depth = 4 |
24 | 25 |
|
25 | | -require_relative '../../harness/loader' |
| 26 | + max_depth = min_depth + 2 if min_depth + 2 > max_depth |
| 27 | + stretch_depth = max_depth + 1 |
26 | 28 |
|
27 | | -run_benchmark(60) do |
28 | | - stretch_tree = bottom_up_tree(stretch_depth) |
29 | | - stretch_tree = nil |
| 29 | + stretch_tree = bottom_up_tree(stretch_depth) |
| 30 | + stretch_tree = nil |
30 | 31 |
|
31 | | - long_lived_tree = bottom_up_tree(max_depth) |
| 32 | + long_lived_tree = bottom_up_tree(max_depth) |
32 | 33 |
|
33 | | - min_depth.step(max_depth, 2) do |depth| |
34 | | - iterations = 2**(max_depth - depth + min_depth) |
| 34 | + min_depth.step(max_depth, 2) do |depth| |
| 35 | + iterations = 2**(max_depth - depth + min_depth) |
35 | 36 |
|
36 | | - check = 0 |
| 37 | + check = 0 |
37 | 38 |
|
38 | | - for i in 1..iterations |
39 | | - temp_tree = bottom_up_tree(depth) |
40 | | - check += item_check(*temp_tree) |
| 39 | + for i in 1..iterations |
| 40 | + temp_tree = bottom_up_tree(depth) |
| 41 | + check += item_check(*temp_tree) |
| 42 | + end |
41 | 43 | end |
42 | 44 | end |
43 | 45 | end |
| 46 | + |
| 47 | +require_relative '../../harness/loader' |
| 48 | + |
| 49 | +run_benchmark(60) do |
| 50 | + Btree.bench |
| 51 | +end |
0 commit comments