Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add benchmarks to compare different structures #415

Merged
merged 1 commit into from
Aug 20, 2021
Merged
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
Add benchmarks to compare different structures
c410-f3r committed Aug 14, 2021

Verified

This commit was signed with the committer’s verified signature.
Freyskeyd Simon Paitrault
commit c7c8e027adaf33eb71cb70a5d3b9de0092ff62f1
13 changes: 11 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
[[bench]]
harness = false
name = "libraries"
path = "benches/libraries.rs"

[package]
authors = ["Paul Mason <paul@form1.co.nz>"]
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

57 changes: 57 additions & 0 deletions benches/libraries.rs
Original file line number Diff line number Diff line change
@@ -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<M, const N: usize>(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);