| 1 | module main |
| 2 | |
| 3 | import os |
| 4 | import scheme |
| 5 | import strconv |
| 6 | import time |
| 7 | import blip.host |
| 8 | import blip.machine |
| 9 | |
| 10 | struct Timing { |
| 11 | elapsed u64 |
| 12 | result i64 |
| 13 | } |
| 14 | |
| 15 | fn measure_vile(c RuntimeCase, stage string) !Timing { |
| 16 | mut vm := scheme.new_with_options(scheme.Options{ max_steps: 100000000, max_frames: 1000000 })! |
| 17 | if stage == 'call' { |
| 18 | vm.eval(c.definitions)! |
| 19 | proc := vm.lookup('benchmark')! |
| 20 | // Clear garbage before the clock; allocation and GC during execution count. |
| 21 | gc_collect() |
| 22 | start := time.sys_mono_now() |
| 23 | result := vm.call(proc, [])! |
| 24 | elapsed := time.sys_mono_now() - start |
| 25 | return Timing{elapsed, result.as_integer()!} |
| 26 | } |
| 27 | source := c.definitions + ' (benchmark)' |
| 28 | gc_collect() |
| 29 | start := time.sys_mono_now() |
| 30 | result := vm.eval(source)! |
| 31 | elapsed := time.sys_mono_now() - start |
| 32 | return Timing{elapsed, result.as_integer()!} |
| 33 | } |
| 34 | |
| 35 | fn measure_blip(c RuntimeCase, stage string) !Timing { |
| 36 | mut vm := machine.new_standalone(host.new_capture()) |
| 37 | vm.max_steps = 100000000 |
| 38 | vm.max_kont = 1000000 |
| 39 | if stage == 'call' { |
| 40 | proc := vm.run_str(c.definitions + ' benchmark')! |
| 41 | gc_collect() |
| 42 | start := time.sys_mono_now() |
| 43 | result := vm.call_value(proc, [])! |
| 44 | elapsed := time.sys_mono_now() - start |
| 45 | return Timing{elapsed, result.as_int()!} |
| 46 | } |
| 47 | source := c.definitions + ' (benchmark)' |
| 48 | gc_collect() |
| 49 | start := time.sys_mono_now() |
| 50 | result := vm.run_str(source)! |
| 51 | elapsed := time.sys_mono_now() - start |
| 52 | return Timing{elapsed, result.as_int()!} |
| 53 | } |
| 54 | |
| 55 | fn verify(c RuntimeCase, vile Timing, blip Timing) ! { |
| 56 | // Explicit checks also run under -prod, which strips V assertions. |
| 57 | if vile.result != c.expected || blip.result != c.expected { |
| 58 | return error('${c.name}: expected ${c.expected}, got Vile ${vile.result}, Blip ${blip.result}') |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | fn median(mut times []u64) f64 { |
| 63 | times.sort() |
| 64 | mid := times.len / 2 |
| 65 | if times.len % 2 == 0 { |
| 66 | return (f64(times[mid - 1]) + f64(times[mid])) / 2 |
| 67 | } |
| 68 | return f64(times[mid]) |
| 69 | } |
| 70 | |
| 71 | fn run_case(c RuntimeCase, stage string, samples int) ! { |
| 72 | verify(c, measure_vile(c, stage)!, measure_blip(c, stage)!)! |
| 73 | mut vile_times := []u64{len: samples} |
| 74 | mut blip_times := []u64{len: samples} |
| 75 | for i in 0 .. samples { |
| 76 | mut vile := Timing{} |
| 77 | mut blip := Timing{} |
| 78 | if i % 2 == 0 { |
| 79 | vile = measure_vile(c, stage)! |
| 80 | blip = measure_blip(c, stage)! |
| 81 | } else { |
| 82 | blip = measure_blip(c, stage)! |
| 83 | vile = measure_vile(c, stage)! |
| 84 | } |
| 85 | verify(c, vile, blip)! |
| 86 | vile_times[i] = vile.elapsed |
| 87 | blip_times[i] = blip.elapsed |
| 88 | } |
| 89 | v := median(mut vile_times) / 1e6 |
| 90 | b := median(mut blip_times) / 1e6 |
| 91 | println('${c.name:15} ${stage:5} ${v:12.3f} ${b:12.3f} ${b / v:12.2f}') |
| 92 | } |
| 93 | |
| 94 | fn main() { |
| 95 | samples := strconv.atoi(if os.args.len > 1 { os.args[1] } else { '9' }) or { panic(err) } |
| 96 | if samples < 1 || samples > 101 { panic('samples must be between 1 and 101') } |
| 97 | println('Identical source and verified integer results; no prelude or I/O.') |
| 98 | println('call: direct host call after definitions; eval: parse + definitions + call.') |
| 99 | println('Fresh VM per sample; VM creation excluded; GC before timing; GC during execution included.') |
| 100 | println('One warmup per implementation/stage; ${samples} samples, alternating order; medians.') |
| 101 | println('case stage Vile ms Blip ms Blip/Vile') |
| 102 | for c in runtime_cases() { |
| 103 | for stage in ['call', 'eval'] { |
| 104 | run_case(c, stage, samples) or { panic(err) } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |