| Crates.io | floating-distance |
| lib.rs | floating-distance |
| version | 0.3.1 |
| created_at | 2023-09-09 17:19:55.404348+00 |
| updated_at | 2023-10-02 04:59:57.966005+00 |
| description | Measure distance between floating-point vectors in Rust |
| homepage | |
| repository | https://github.com/AsherJingkongChen/floating-distance.git |
| max_upload_size | |
| id | 968260 |
| size | 42,308 |
This is a Rust library. Here you can do:
cargo add floating-distance
cargo run --example default
v0 and v1use floating_distance::*;
fn main() {
let v0: &[f32] = &[1.0, 2.0, 2.0, 1.0, 2.0, 1.0, 1.0];
let v1: &[f32] = &[2.0, 1.0, 1.0, 1.0, 2.0, 1.0, 2.0];
let metric = Metric::Cosine;
let result = metric.measure::<f32>(v0, v1);
let expectation: f32 = 14.0 / (4.0 * 4.0);
assert_eq!(result, expectation);
}
SIMD is the acronym for Single Instruction Multiple Data
Modern CPUs have special instructions. We can use them to accelerate vector computations!
You can enable simd feature in this crate by the following steps:
features = ["simd"] in Cargo.toml manifest:[dependencies]
floating-distance = { version = "*", features = ["simd"] }
rust-toolchain.toml, which is next to Cargo.toml:[toolchain]
channel = "nightly"
RUSTFLAGS environment variable and -C target-feature compiler option like these:RUSTFLAGS="-C target-feature=+ssse3" cargo build
RUSTFLAGS="-C target-feature=+avx,+sse3" cargo build --release
You can find all target features of Rust by this:
rustc --print target-features
I have run a simple benchmark on my laptop (architecture: aarch64-apple-darwin).
The SIMD instruction set is NEON.
Let's check out the results first!
no_simd: 198,306 ns/iter (+/- 5,173)
simd: 18,430 ns/iter (+/- 655)
| Type | Avarage time (ns/iter) | Rate (relative) |
|---|---|---|
| No SIMD | 198306 | 1.00 |
| SIMD | 18430 | 10.76 |
As the data shown, we can see that SIMD can improve the performance significantly!
You can also benchmark it by repeating the following steps:
.cargo/config.toml(cargo +nightly bench -p benchmarks-no-simd &&
cargo +nightly bench -p benchmarks-simd) 2> /dev/null
portable-simd.