Experiment 90635AE: Reduce duration
Base a0817e0 Metric ID bbf2b1a
rad-experiment show 90635ae2fde817af360fd8c824fc148ec2566a28
by vagrant · Mar 31 15:10 2026
Fast doubling O(log n) with sum identity F(n+1)-1 — fewer iterations but more arithmetic per step
Delta-0.00%
Metricduration (ms)
Criterialower_is_better
LOC+28 -16
-010203040base123456789101112131415EXPERIMENTS
Kept Discarded Running best
Measurements
MetricBaselineCandidateDelta
duration (ms)How long the code takes to run. Less is better.0.000 ms (n=5)0.000 ms (n=5)-0.00%
ops_per_sec (ops/sec)Operations completed per second. More is better.1999995.950 ops/sec1715264.450 ops/sec-14.23% REGRESSED
p95 (ms)Measures p95 in ms. Less is better.0.000 ms0.090 ms-0.00%
Diff
~ src/index.ts
@@ -3,25 +3,37 @@
3 * 4 * For n = 30, the expected result is 1,346,268. 5 * 6- * Uses iterative O(n) approach with running sum — no recursion, 7- * no string conversions, single pass. 6+ * Uses the identity: sum(F(0)..F(n-1)) = F(n+1) - 1 7+ * F(n+1) computed via fast doubling in O(log n). 8 */ 9 export function sumFibonacci(n: number): number { 10 if (n <= 0) return 0; 11+ // sum(F(0)..F(n-1)) = F(n+1) - 1 12+ return fibFast(n + 1) - 1; 13+} 14 12- let sum = 0; 13- let prev = 0; 14- let curr = 1; 15- 16- // F(0) = 0 17- sum += prev; 18- 19- for (let i = 1; i < n; i++) { 20- sum += curr; 21- const next = prev + curr; 22- prev = curr; 23- curr = next; 15+/** 16+ * Fast doubling: computes F(n) in O(log n) time. 17+ * Returns F(n) using the identities: 18+ * F(2k) = F(k) * (2*F(k+1) - F(k)) 19+ * F(2k+1) = F(k)^2 + F(k+1)^2 20+ */ 21+function fibFast(n: number): number { 22+ let a = 0; // F(0) 23+ let b = 1; // F(1) 24+ let bit = 1 << (31 - Math.clz32(n)); 25+ while (bit > 0) { 26+ // doubling step 27+ const d = a * (2 * b - a); 28+ const e = a * a + b * b; 29+ a = d; 30+ b = e; 31+ if (n & bit) { 32+ const c = a + b; 33+ a = b; 34+ b = c; 35+ } 36+ bit >>>= 1; 37 } 25- 26- return sum; 38+ return a; 39 }
Environment
Base commita0817e08e11da271bb7fe56d8de9648a4fcbd977
Candidate commitd048056628027534c09cab1f036d28c7ca9a8b09
Buildtrue
Teststrue
Metric IDbbf2b1a
Archaarch64
OSLinux 6.8.0-86-generic
CPU-
Agentclaude-code / claude-opus-4-6
Filessrc/index.ts