Skip to main content

euclidean_distance

Function euclidean_distance 

Source
pub fn euclidean_distance(a: &[f32], b: &[f32]) -> f32
Expand description

Runtime-dispatched Euclidean (L2) distance computation

Automatically selects AVX2 or scalar implementation based on CPU feature detection at runtime. Provides optimal performance on AVX2-capable hardware while maintaining compatibility with all platforms.

§Behavior

  • x86_64 with AVX2: Uses euclidean_distance_avx2 (8x parallelism)
  • Other platforms: Uses euclidean_distance_scalar (fallback)
  • Detection: Cached after first check (no repeated overhead)

§Arguments

  • a - First vector slice
  • b - Second vector slice (must have same length as a)

§Returns

Euclidean distance (L2 norm) >= 0

§Performance

  • AVX2: ~8x speedup for large vectors
  • Scalar: Baseline performance (same as iterator-based)
  • Detection overhead: O(1) after first call