diff --git a/Cargo.toml b/Cargo.toml index c336d2b8..d1cf6d70 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,16 +1,21 @@ +[[bench]] +harness = false +name = "libraries" +path = "benches/libraries.rs" + [package] authors = ["Paul Mason "] categories = ["science","data-structures"] description = "A Decimal Implementation written in pure Rust suitable for financial calculations." documentation = "https://docs.rs/rust_decimal/" edition = "2018" +exclude = [ "tests/generated/*" ] keywords = ["decimal","financial","fixed","precision"] license = "MIT" name = "rust_decimal" readme = "./README.md" repository = "https://github.com/paupino/rust-decimal" version = "1.15.0" -exclude = [ "tests/generated/*" ] [package.metadata.docs.rs] all-features = true @@ -28,10 +33,14 @@ serde_json = { default-features = false, optional = true, version = "1.0" } tokio-postgres = { default-features = false, optional = true, version = "0.7" } [dev-dependencies] +bigdecimal = { default-features = false, version = "0.2" } bincode = "1.3" bytes = "1.0" +criterion = { default-features = false, version = "0.3" } csv = "1" +decimal_rs = { default-features = false, package = "decimal", version = "2.0" } futures = "0.3" +rust_decimal_macros = { path = "macros" } serde_derive = "1.0" serde_json = "1.0" tokio = { features = ["rt-multi-thread", "test-util", "macros"], version = "1.0" } @@ -46,10 +55,10 @@ legacy-ops = [] maths = [] maths-nopanic = ["maths"] rust-fuzz = ["arbitrary"] +serde-arbitrary-precision = ["serde", "serde_json/arbitrary_precision"] serde-bincode = ["serde-str"] # Backwards compatability serde-float = ["serde"] serde-str = ["serde"] -serde-arbitrary-precision = ["serde", "serde_json/arbitrary_precision"] std = ["arrayvec/std"] tokio-pg = ["db-tokio-postgres"] # Backwards compatability diff --git a/benches/libraries.rs b/benches/libraries.rs new file mode 100644 index 00000000..0587b9cb --- /dev/null +++ b/benches/libraries.rs @@ -0,0 +1,57 @@ +use core::str::FromStr; +use criterion::{ + black_box, criterion_group, criterion_main, measurement::Measurement, BenchmarkGroup, BenchmarkId, Criterion, +}; + +macro_rules! add_benchmark_group { + ($criterion:expr, $f:ident, $op:tt) => { + fn $f(group: &mut BenchmarkGroup<'_, M>) + where + M: Measurement, + { + group.bench_with_input(BenchmarkId::new("bigdecimal", N), &N, |ben, _| { + let a = bigdecimal::BigDecimal::from_str("2.01").unwrap(); + let b = bigdecimal::BigDecimal::from_str("2.01").unwrap(); + ben.iter(|| black_box(a.clone() $op b.clone())) + }); + + group.bench_with_input(BenchmarkId::new("decimal-rs", N), &N, |ben, _| { + let a = decimal_rs::d128!(2.01); + let b = decimal_rs::d128!(2.01); + ben.iter(|| black_box(a $op b)) + }); + + group.bench_with_input(BenchmarkId::new("f32", N), &N, |ben, _| { + let a = 2.01f32; + let b = 2.01f32; + ben.iter(|| black_box(a $op b)) + }); + + group.bench_with_input(BenchmarkId::new("f64", N), &N, |ben, _| { + let a = 2.01f64; + let b = 2.01f64; + ben.iter(|| black_box(a $op b)) + }); + + group.bench_with_input(BenchmarkId::new("rust-decimal", N), &N, |ben, _| { + let a = rust_decimal_macros::dec!(2.01); + let b = rust_decimal_macros::dec!(2.01); + ben.iter(|| black_box(a $op b)) + }); + } + + let mut group = $criterion.benchmark_group(stringify!($f)); + $f::<_, 100>(&mut group); + group.finish(); + }; +} + +fn criterion_benchmark(c: &mut Criterion) { + add_benchmark_group!(c, addition, +); + add_benchmark_group!(c, division, /); + add_benchmark_group!(c, multiplication, *); + add_benchmark_group!(c, subtraction, -); +} + +criterion_group!(benches, criterion_benchmark); +criterion_main!(benches);