Precision: f32 vs f64
Bessel functions involve intensive iterative algorithms, recurrence relations, and transcendental operations. As a result, calculating them in double precision (f64) requires significantly more computational power than single precision (f32).
Many real-world applications in computer graphics, audio processing, or machine learning only require 32 bits of precision. amos-bessel-rs provides generic algorithms bound by the BesselFloat trait, meaning you can drop in f32 natively and gain significant performance benefits.
Benchmark Results
Note: All benchmarks were executed on an Apple M2 Pro (ARM64) using Rust 1.95.0. While absolute times will vary depending on your hardware, the comparative speedups should remain relatively consistent across modern CPU architectures.
Below is a direct comparison of f32 vs f64 evaluation times using the Criterion framework. The benchmarks are split into two datasets:
- Scattered: An array of 46 highly extreme edge cases designed to trigger the slowest algorithmic paths (e.g., asymptotic expansions vs Miller algorithms).
- Grid: A realistic uniform grid of thousands of sequential points, designed to test typical throughput, caching, and branch prediction.
Performance Comparison: f32 vs f64
------------------------------------------------------------
Function | Dataset | f32 (ns) | f64 (ns) | Speedup
------------------------------------------------------------
J | Scattered | 9192.55 | 17313.19 | 1.88x
J | Grid | 4892837.96 | 9198335.20 | 1.88x
Y | Scattered | 15794.43 | 35795.55 | 2.27x
Y | Grid | 9955819.66 | 19215359.98| 1.93x
I | Scattered | 6398.72 | 15119.08 | 2.36x
I | Grid | 4991749.35 | 8964225.62 | 1.80x
K | Scattered | 9390.94 | 18567.97 | 1.98x
K | Grid | 6510330.20 | 14086508.75| 2.16x
H1 | Scattered | 10777.64 | 24668.69 | 2.29x
H1 | Grid | 6664222.40 | 14094536.78| 2.11x
H2 | Scattered | 12647.01 | 24028.28 | 1.90x
H2 | Grid | 6791329.33 | 14324728.55| 2.11x
Ai | Scattered | 2740.28 | 6914.27 | 2.52x
Ai | Grid | 256551.19 | 568103.49 | 2.21x
Bi | Scattered | 2724.28 | 6913.83 | 2.54x
Bi | Grid | 375262.96 | 863576.61 | 2.30x
------------------------------------------------------------
Overall Geometric Mean Speedup (f32 over f64): 2.13x
Analysis
As shown above, f32 is on average over twice as fast (2.13x) as f64 across the entire suite of functions.
For the Airy functions (Ai, Bi), the speedup is particularly pronounced (approaching ~2.5x), reflecting their reliance on computationally heavy cube roots and fractional powers which execute much faster on f32 ALUs.
Recommendation: Unless your scientific workload strictly requires 64-bit IEEE 754 precision, you should default to f32 arguments when evaluating large quantities of Bessel functions.