Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Generated by Cargo
# will have compiled files and executables
/target/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Expand Down
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ rust-version = "1.88"
bench = false

[dependencies]

bnum = { version = "0.12.0", default-features = false }

num-integer = { version = "0.1", default-features = false, optional = true }
num-traits = { version = "0.2", default-features = false, optional = true }
zeroize = { version = "1.6", default-features = false, optional = true }
Expand Down
3 changes: 2 additions & 1 deletion benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
license = "MIT OR Apache-2.0"

[dev-dependencies]
criterion = { version = "0.6.0" }
criterion = { version = "0.8.1" }
fastnum = { path = "../" }
bnum = { version = "0.12.0", default-features = false, features = ["numtraits"] }
num-integer = { version = "0.1", default-features = false }
Expand All @@ -15,6 +15,7 @@ rust_decimal = { version = "1.37.1", features = ["maths"] }
num-traits = "0.2.19"
rust_decimal_macros = "1.37.1"
ryu = "1.0"
fixed-num = { version = "0.2.0" }

[[bench]]
name = "decimal_add"
Expand Down
14 changes: 13 additions & 1 deletion benchmark/benches/decimal/_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ macro_rules! benchmark_op {
|bench, (a, b)| bench.iter(|| black_box(*a) $op black_box(*b)),
);

// fixed-num Dec19x19: only benchmarks values within its range (±17_014_118_346_046_923_173.168...)
if let (Ok(a_fn), Ok(b_fn)) = (
fixed_num::Dec19x19::from_str($a),
fixed_num::Dec19x19::from_str($b),
) {
$group.bench_with_input(
BenchmarkId::new("fixed_num", $case),
&(a_fn, b_fn),
|bench, (a, b)| bench.iter(|| black_box(*a) $op black_box(*b)),
);
}

benchmark_op!(@ A $op, $case, $group, [$($bits),*], $a, $b);
}};
(@ A $op: tt, $case: literal, $group: ident, [$($bits: literal),*], $a: literal, $b: literal) => {{
Expand Down Expand Up @@ -52,4 +64,4 @@ macro_rules! benchmark_op {
}};
}

pub(super) use benchmark_op;
pub(super) use benchmark_op;
9 changes: 8 additions & 1 deletion benchmark/benches/decimal/allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::{hint::black_box, str::FromStr};

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};

extern crate fixed_num;

criterion_group!(allocate, vector);
criterion_main!(allocate);

Expand All @@ -15,7 +17,12 @@ fn vector(c: &mut Criterion) {
});

group.bench_with_input(BenchmarkId::new("rust_decimal", size), &size, |b, size| {
let n = rust_decimal::Decimal::from_str("0.12345678910111213").unwrap();
let n = rust_decimal::Decimal::from_str("0.12345678910111213").unwrap();
b.iter(|| vec![black_box(n); black_box(*size)])
});

group.bench_with_input(BenchmarkId::new("fixed_num", size), &size, |b, size| {
let n = fixed_num::Dec19x19::from_str("0.12345678910111213").unwrap();
b.iter(|| vec![black_box(n); black_box(*size)])
});

Expand Down
10 changes: 9 additions & 1 deletion benchmark/benches/decimal/from_f64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{hint::black_box};
use std::hint::black_box;

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};

Expand All @@ -16,6 +16,14 @@ macro_rules! macro_impl {
$group.bench_with_input(BenchmarkId::new("rust_decimal", $size), &$size, |b, _| {
b.iter(|| rust_decimal::Decimal::from_f64_retain(black_box($f)).unwrap())
});

// fixed-num Dec19x19: try_from for f64
if let Ok(_) = fixed_num::Dec19x19::try_from($f) {
$group.bench_with_input(BenchmarkId::new("fixed_num", $size), &$size, |b, _| {
b.iter(|| fixed_num::Dec19x19::try_from(black_box($f)).unwrap())
});
}

macro_impl!(@ A $group, [$($bits),*], $f, $size);
}};
(@ A $group: ident, [$($bits: literal),*], $f: expr, $size: ident) => {{
Expand Down
8 changes: 8 additions & 0 deletions benchmark/benches/decimal/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ macro_rules! macro_impl {
$group.bench_with_input(BenchmarkId::new("rust_decimal", $size), &$size, |b, _| {
b.iter(|| rust_decimal::Decimal::from_str(black_box($str)).unwrap())
});

// fixed-num Dec19x19: only benchmark if value is within range
if let Ok(_) = fixed_num::Dec19x19::from_str($str) {
$group.bench_with_input(BenchmarkId::new("fixed_num", $size), &$size, |b, _| {
b.iter(|| fixed_num::Dec19x19::from_str(black_box($str)).unwrap())
});
}

macro_impl!(@ A $group, [$($bits),*], $str, $size);
}};
(@ A $group: ident, [$($bits: literal),*], $str: ident, $size: ident) => {{
Expand Down
53 changes: 30 additions & 23 deletions benchmark/benches/decimal/recip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,36 @@ macro_rules! macro_impl {

let a_rd = rust_decimal::Decimal::from_str($a).unwrap();

$group.bench_with_input(
BenchmarkId::new("f64", size),
&a_f64,
|bench, a| bench.iter(|| black_box(*a).recip()),
);

$group.bench_with_input(
BenchmarkId::new("rust_decimal", size),
&a_rd,
|bench, a| bench.iter(|| rust_decimal::Decimal::ONE.checked_div(black_box(*a)).unwrap()),
);

$group.bench_with_input(
BenchmarkId::new("fastnum", size),
&a,
|bench, a| bench.iter(|| black_box(*a).recip()),
);

$group.bench_with_input(
BenchmarkId::new("bigdecimal", size),
&a_bd,
|bench, a| bench.iter(|| black_box(a).inverse()),
);
$group.bench_with_input(BenchmarkId::new("f64", size), &a_f64, |bench, a| {
bench.iter(|| black_box(*a).recip())
});

$group.bench_with_input(BenchmarkId::new("rust_decimal", size), &a_rd, |bench, a| {
bench.iter(|| {
rust_decimal::Decimal::ONE
.checked_div(black_box(*a))
.unwrap()
})
});

// fixed-num Dec19x19: only benchmark if value is within range
// Dec19x19 doesn't have recip(), use 1/x via division
if let Ok(a_fn) = fixed_num::Dec19x19::from_str($a) {
let one_fn = fixed_num::Dec19x19::from(1_i32);
$group.bench_with_input(
BenchmarkId::new("fixed_num", size),
&(one_fn, a_fn),
|bench, (one, a)| bench.iter(|| black_box(*one) / black_box(*a)),
);
}

$group.bench_with_input(BenchmarkId::new("fastnum", size), &a, |bench, a| {
bench.iter(|| black_box(*a).recip())
});

$group.bench_with_input(BenchmarkId::new("bigdecimal", size), &a_bd, |bench, a| {
bench.iter(|| black_box(a).inverse())
});
}};
}

Expand Down
6 changes: 6 additions & 0 deletions benchmark/benches/decimal/sqrt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ fn bench(c: &mut Criterion) {
let x_f64 = 3.0_f64;
let x_bd = bigdecimal::BigDecimal::try_from(3.0_f64).unwrap();
let x_rd = rust_decimal::Decimal::try_from(3.0_f64).unwrap();
let x_fn = fixed_num::Dec19x19::from(3_i32);

group.bench_with_input(BenchmarkId::new("f64", "3"), &x_f64, |bench, x| {
bench.iter(|| black_box(x).sqrt())
Expand All @@ -21,6 +22,11 @@ fn bench(c: &mut Criterion) {
bench.iter(|| black_box(x).sqrt())
});

group.bench_with_input(BenchmarkId::new("fixed_num", "3"), &x_fn, |bench, x| {
use fixed_num::ops::UncheckedSqrt;
bench.iter(|| black_box(*x).unchecked_sqrt())
});

group.bench_with_input(BenchmarkId::new("fastnum", "3"), &x, |bench, x| {
bench.iter(|| black_box(x).sqrt())
});
Expand Down
8 changes: 8 additions & 0 deletions benchmark/benches/decimal/to_f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ macro_rules! macro_impl {
$group.bench_with_input(BenchmarkId::new("rust_decimal", $size), &$size, |b, _| {
b.iter(|| black_box(n).to_f64().unwrap());
});

// fixed-num Dec19x19: only benchmark if value is within range
if let Ok(n_fn) = fixed_num::Dec19x19::from_str($str) {
$group.bench_with_input(BenchmarkId::new("fixed_num", $size), &$size, |b, _| {
b.iter(|| f64::from(black_box(n_fn)))
});
}

macro_impl!(@ A $group, [$($bits),*], $str, $size);
}};
(@ A $group: ident, [$($bits: literal),*], $str: ident, $size: ident) => {{
Expand Down