Floating Point Comparison Benchmark
A lightweight experiment comparing different approaches to floating-point number comparison in Rust.
Objective
This project aims to explore whether a minimal floating-point representation focused solely on comparison operations can outperform the more comprehensive Decimal from the rust_decimal crate for ordering/comparison use cases.
The hypothesis is that by stripping away all the overhead required for mathematical operations and focusing purely on comparison logic, we might achieve better performance for scenarios where only ordering matters.
Approach
The project implements two main approaches:
- Using
Decimal- A full-featured decimal floating-point implementation - Using
Float- A minimal custom implementation that:- Stores numbers as (mantissa, exponent, sign) triplets
- Only implements comparison operations
- Skips all the complexity needed for arithmetic operations
Benchmarks
The project uses Criterion.rs for benchmarking. To run the benchmarks:
This will compare the performance of:
- Comparing numbers using
Decimal - Comparing numbers using our minimal
Floatimplementation - Converting between
f64andFloat - Converting between
f64andDecimal
Usage
// Create a new Float
let a = from_f64.unwrap;
let b = from_f64.unwrap;
// Compare the two numbers
assert!;
// Convert back if needed
let a_f64 = a.to_f64;
let b_f64 = b.to_f64;
Project Structure
src/lib.rs- CoreFloatimplementationsrc/main.rs- Simple demonstration programbenches/float_comparison.rs- Benchmarking code
Contributing
This is an experimental project aimed at exploring a specific performance hypothesis. Feel free to:
- Run the benchmarks on your own hardware
- Suggest optimizations to the minimal implementation
- Add additional comparison approaches
License
MIT License
Acknowledgments
Thanks to the rust_decimal crate for providing a great reference implementation for decimal floating-point numbers in Rust.