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 #416

Merged
merged 2 commits into from
Aug 22, 2021
Merged
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
10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
[[bench]]
harness = false
name = "comparison"
path = "benches/comparison.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
Expand All @@ -30,6 +35,7 @@ tokio-postgres = { default-features = false, optional = true, version = "0.7" }
[dev-dependencies]
bincode = "1.3"
bytes = "1.0"
criterion = { default-features = false, version = "0.3" }
csv = "1"
futures = "0.3"
serde_derive = "1.0"
Expand All @@ -46,10 +52,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

Expand Down
50 changes: 50 additions & 0 deletions benches/comparison.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//! See how `rust-decimal` performs compared to native floating numbers.

use criterion::{
black_box, criterion_group, criterion_main, measurement::Measurement, BenchmarkGroup, BenchmarkId, Criterion,
};
use rust_decimal::Decimal;

const DECIMAL_2_01: Decimal = Decimal::from_parts(201, 0, 0, false, 2);
const DECIMAL_13_7: Decimal = Decimal::from_parts(137, 0, 0, false, 1);
const F64_2_01: f64 = 2.01;
const F64_13_7: f64 = 13.7;

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("f64 (diff)", N), &N, |ben, _| {
ben.iter(|| black_box(F64_2_01 $op F64_13_7))
});

group.bench_with_input(BenchmarkId::new("f64 (equal)", N), &N, |ben, _| {
ben.iter(|| black_box(F64_2_01 $op F64_2_01))
});

group.bench_with_input(BenchmarkId::new("rust-decimal (diff)", N), &N, |ben, _| {
ben.iter(|| black_box(DECIMAL_2_01 $op DECIMAL_13_7))
});

group.bench_with_input(BenchmarkId::new("rust-decimal (equal)", N), &N, |ben, _| {
ben.iter(|| black_box(DECIMAL_2_01 $op DECIMAL_2_01))
});
}

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);