|
| 1 | +use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion}; |
| 2 | +use phyllotactic_manifold::*; |
| 3 | + |
| 4 | +fn make_seeds(n: usize) -> Vec<[i8; 34]> { |
| 5 | + let mut seeds = Vec::with_capacity(n); |
| 6 | + for i in 0..n { |
| 7 | + let mut seed = [0i8; 34]; |
| 8 | + seed[0] = (i % 128) as i8; // HEEL |
| 9 | + seed[33] = ((i * 7) % 128) as i8; // GAMMA |
| 10 | + for j in 1..33 { |
| 11 | + seed[j] = ((i * 13 + j * 37) % 256) as i8; |
| 12 | + } |
| 13 | + seeds.push(seed); |
| 14 | + } |
| 15 | + seeds |
| 16 | +} |
| 17 | + |
| 18 | +fn bench_encode(c: &mut Criterion) { |
| 19 | + let seeds = make_seeds(1024); |
| 20 | + let mut group = c.benchmark_group("encode"); |
| 21 | + |
| 22 | + group.bench_function("flat8", |b| { |
| 23 | + b.iter(|| { |
| 24 | + for seed in &seeds { |
| 25 | + black_box(flat8::encode(black_box(seed))); |
| 26 | + } |
| 27 | + }) |
| 28 | + }); |
| 29 | + |
| 30 | + group.bench_function("spiral8", |b| { |
| 31 | + b.iter(|| { |
| 32 | + for seed in &seeds { |
| 33 | + black_box(spiral8::encode(black_box(seed))); |
| 34 | + } |
| 35 | + }) |
| 36 | + }); |
| 37 | + |
| 38 | + group.bench_function("spiral8_gamma", |b| { |
| 39 | + b.iter(|| { |
| 40 | + for seed in &seeds { |
| 41 | + black_box(spiral8_gamma::encode(black_box(seed))); |
| 42 | + } |
| 43 | + }) |
| 44 | + }); |
| 45 | + |
| 46 | + group.bench_function("seven_plus_one", |b| { |
| 47 | + b.iter(|| { |
| 48 | + for seed in &seeds { |
| 49 | + black_box(seven_plus_one::encode(black_box(seed))); |
| 50 | + } |
| 51 | + }) |
| 52 | + }); |
| 53 | + |
| 54 | + group.finish(); |
| 55 | +} |
| 56 | + |
| 57 | +fn bench_resonance(c: &mut Criterion) { |
| 58 | + let seeds = make_seeds(1024); |
| 59 | + let threshold = 1000.0; |
| 60 | + |
| 61 | + // Pre-encode |
| 62 | + let flat8_enc: Vec<_> = seeds.iter().map(|s| flat8::encode(s)).collect(); |
| 63 | + let spiral8_enc: Vec<_> = seeds.iter().map(|s| spiral8::encode(s)).collect(); |
| 64 | + let spiral8g_enc: Vec<_> = seeds.iter().map(|s| spiral8_gamma::encode(s)).collect(); |
| 65 | + let s7p1_enc: Vec<_> = seeds.iter().map(|s| seven_plus_one::encode(s)).collect(); |
| 66 | + |
| 67 | + let mut group = c.benchmark_group("resonance"); |
| 68 | + |
| 69 | + group.bench_function("flat8", |b| { |
| 70 | + b.iter(|| { |
| 71 | + for enc in &flat8_enc { |
| 72 | + black_box(flat8::resonance(black_box(enc), threshold)); |
| 73 | + } |
| 74 | + }) |
| 75 | + }); |
| 76 | + |
| 77 | + group.bench_function("spiral8", |b| { |
| 78 | + b.iter(|| { |
| 79 | + for (x, y) in &spiral8_enc { |
| 80 | + black_box(spiral8::resonance(black_box(x), black_box(y), threshold)); |
| 81 | + } |
| 82 | + }) |
| 83 | + }); |
| 84 | + |
| 85 | + group.bench_function("spiral8_gamma", |b| { |
| 86 | + b.iter(|| { |
| 87 | + for (x, y) in &spiral8g_enc { |
| 88 | + black_box(spiral8_gamma::resonance( |
| 89 | + black_box(x), |
| 90 | + black_box(y), |
| 91 | + threshold, |
| 92 | + )); |
| 93 | + } |
| 94 | + }) |
| 95 | + }); |
| 96 | + |
| 97 | + group.bench_function("seven_plus_one", |b| { |
| 98 | + b.iter(|| { |
| 99 | + for m in &s7p1_enc { |
| 100 | + black_box(seven_plus_one::resonance(black_box(m), threshold)); |
| 101 | + } |
| 102 | + }) |
| 103 | + }); |
| 104 | + |
| 105 | + group.finish(); |
| 106 | +} |
| 107 | + |
| 108 | +fn bench_clam48(c: &mut Criterion) { |
| 109 | + let seeds = make_seeds(1024); |
| 110 | + let manifolds: Vec<_> = seeds.iter().map(|s| seven_plus_one::encode(s)).collect(); |
| 111 | + |
| 112 | + c.bench_function("clam48_extraction", |b| { |
| 113 | + b.iter(|| { |
| 114 | + for m in &manifolds { |
| 115 | + black_box(seven_plus_one::to_clam48(black_box(m), 100.0, 1e8)); |
| 116 | + } |
| 117 | + }) |
| 118 | + }); |
| 119 | +} |
| 120 | + |
| 121 | +fn bench_dead_zone(c: &mut Criterion) { |
| 122 | + let seed = { |
| 123 | + let mut s = [0i8; 34]; |
| 124 | + s[0] = 42; |
| 125 | + s[33] = 7; |
| 126 | + for i in 1..33 { |
| 127 | + s[i] = (i as i8).wrapping_mul(13).wrapping_add(37); |
| 128 | + } |
| 129 | + s |
| 130 | + }; |
| 131 | + |
| 132 | + c.bench_function("dead_zone_full_272bit", |b| { |
| 133 | + b.iter(|| { |
| 134 | + black_box(dead_zone::run_benchmark(black_box(&seed), 100.0)); |
| 135 | + }) |
| 136 | + }); |
| 137 | +} |
| 138 | + |
| 139 | +fn bench_encode_scaling(c: &mut Criterion) { |
| 140 | + let mut group = c.benchmark_group("encode_scaling"); |
| 141 | + for n in [64, 256, 1024, 4096] { |
| 142 | + let seeds = make_seeds(n); |
| 143 | + group.bench_with_input(BenchmarkId::new("seven_plus_one", n), &seeds, |b, seeds| { |
| 144 | + b.iter(|| { |
| 145 | + for seed in seeds { |
| 146 | + black_box(seven_plus_one::encode(black_box(seed))); |
| 147 | + } |
| 148 | + }) |
| 149 | + }); |
| 150 | + } |
| 151 | + group.finish(); |
| 152 | +} |
| 153 | + |
| 154 | +criterion_group!( |
| 155 | + benches, |
| 156 | + bench_encode, |
| 157 | + bench_resonance, |
| 158 | + bench_clam48, |
| 159 | + bench_dead_zone, |
| 160 | + bench_encode_scaling, |
| 161 | +); |
| 162 | +criterion_main!(benches); |
0 commit comments