Hasty Briefsbeta

Faster Argmin on Floats

2 days ago
  • #performance
  • #floating-point
  • #optimization
  • Problem: Find the index of the smallest floating-point number in a large array, with all values being positive or +0, non-infinity, and non-NaN.
  • First solution: Uses `total_cmp` for comparison, runs in ~511 μs for a million numbers.
  • Second solution: Implements a custom comparator using natural partial order, runs in ~489 μs.
  • Third solution: Uses `partial_cmp` with unwrap, slightly faster at ~470 μs.
  • Fourth solution: Leverages floating-point representation by comparing `u32` bits, fastest at ~370 μs (30% speedup).
  • Benchmark program provided to compare the performance of each solution.