Four Ways to Find the Argmin of Floats in Rust (and Their Performance)
2025-09-20
This article explores four methods for quickly finding the index of the minimum value in a large array of floating-point numbers in Rust. The first method uses `min_by` and `total_cmp`, taking 511 microseconds; the second uses `reduce`, taking 489 microseconds; the third uses `partial_cmp`, taking 470 microseconds; and the fourth leverages the bit representation of positive floats, converting them to `u32` for comparison, taking only 370 microseconds – a 30% speedup. The fourth method proves most efficient for arrays containing only positive numbers, cleverly exploiting the internal representation of floats to avoid complex comparisons.
Development