Skip to content

Commit 3103190

Browse files
author
Jacob Zhong
committed
Add benchmarks
1 parent 29dc93a commit 3103190

8 files changed

Lines changed: 354 additions & 95 deletions

File tree

float/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,8 @@ required-features = ["postgres-types", "diesel_v1", "diesel_v2", "diesel_v2/post
8383
name = "primitive"
8484
required-features = ["rand"]
8585
harness = false
86+
87+
[[bench]]
88+
name = "io"
89+
required-features = ["rand"]
90+
harness = false

float/benches/io.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//! IO benchmarks (printing and parsing).
2+
//! Run: cargo bench -p dashu-float --bench io --features rand -- --quick
3+
4+
use criterion::{
5+
criterion_group, criterion_main, AxisScale, BenchmarkId, Criterion, PlotConfiguration,
6+
};
7+
use dashu_base::Sign;
8+
use dashu_float::DBig;
9+
use dashu_int::{IBig, UBig};
10+
use rand_v08::prelude::*;
11+
use std::fmt::Write;
12+
13+
const SEED: u64 = 1;
14+
15+
fn random_dbig<R>(precision: usize, rng: &mut R) -> DBig
16+
where
17+
R: Rng + ?Sized,
18+
{
19+
let precision_ub = UBig::from_word(10).pow(precision + 1);
20+
let precision_lb = UBig::from_word(10).pow(precision);
21+
let significand = rng.gen_range(precision_lb..precision_ub);
22+
let sign = Sign::from(rng.gen_bool(0.5));
23+
let exponent = rng.gen_range(-(precision as isize)..(precision as isize));
24+
DBig::from_parts(IBig::from_parts(sign, significand), exponent)
25+
}
26+
27+
fn dbig_to_string(criterion: &mut Criterion) {
28+
let mut rng = StdRng::seed_from_u64(SEED);
29+
let mut group = criterion.benchmark_group("dbig_to_string");
30+
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
31+
32+
for log_prec in 1..=5 {
33+
let precision = 10usize.pow(log_prec);
34+
let a = random_dbig(precision, &mut rng);
35+
let mut out = String::new();
36+
group.bench_with_input(BenchmarkId::from_parameter(precision), &a, |bencher, ta| {
37+
bencher.iter(|| {
38+
out.clear();
39+
write!(&mut out, "{}", ta).unwrap();
40+
out.len()
41+
})
42+
});
43+
}
44+
45+
group.finish();
46+
}
47+
48+
fn dbig_from_str(criterion: &mut Criterion) {
49+
let mut rng = StdRng::seed_from_u64(SEED);
50+
let mut group = criterion.benchmark_group("dbig_from_str");
51+
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
52+
53+
for log_prec in 1..=5 {
54+
let precision = 10usize.pow(log_prec);
55+
let a = random_dbig(precision, &mut rng);
56+
let s = a.to_string();
57+
group.bench_with_input(BenchmarkId::from_parameter(precision), &s, |bencher, ts| {
58+
bencher.iter(|| ts.parse::<DBig>())
59+
});
60+
}
61+
62+
group.finish();
63+
}
64+
65+
fn dbig_scientific_fmt(criterion: &mut Criterion) {
66+
let mut rng = StdRng::seed_from_u64(SEED);
67+
let mut group = criterion.benchmark_group("dbig_scientific_fmt");
68+
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
69+
70+
for log_prec in 1..=5 {
71+
let precision = 10usize.pow(log_prec);
72+
let a = random_dbig(precision, &mut rng);
73+
let mut out = String::new();
74+
group.bench_with_input(BenchmarkId::from_parameter(precision), &a, |bencher, ta| {
75+
bencher.iter(|| {
76+
out.clear();
77+
write!(&mut out, "{:e}", ta).unwrap();
78+
out.len()
79+
})
80+
});
81+
}
82+
83+
group.finish();
84+
}
85+
86+
criterion_group!(benches, dbig_to_string, dbig_from_str, dbig_scientific_fmt,);
87+
88+
criterion_main!(benches);

integer/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,8 @@ required-features = ["num-order"]
6969
name = "primitive"
7070
required-features = ["rand"]
7171
harness = false
72+
73+
[[bench]]
74+
name = "io"
75+
required-features = ["rand"]
76+
harness = false

integer/benches/io.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//! IO benchmarks (printing and parsing).
2+
//! Run: cargo bench -p dashu-int --bench io --features rand -- --quick
3+
4+
use criterion::{
5+
criterion_group, criterion_main, AxisScale, BenchmarkId, Criterion, PlotConfiguration,
6+
};
7+
use dashu_int::UBig;
8+
use rand_v08::prelude::*;
9+
use std::fmt::Write;
10+
11+
const SEED: u64 = 1;
12+
13+
fn random_ubig<R>(bits: usize, rng: &mut R) -> UBig
14+
where
15+
R: Rng + ?Sized,
16+
{
17+
rng.gen_range(UBig::ONE << (bits - 1)..UBig::ONE << bits)
18+
}
19+
20+
fn ubig_to_hex(criterion: &mut Criterion) {
21+
let mut rng = StdRng::seed_from_u64(SEED);
22+
let mut group = criterion.benchmark_group("ubig_to_hex");
23+
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
24+
25+
for log_bits in 1..=6 {
26+
let bits = 10usize.pow(log_bits);
27+
let a = random_ubig(bits, &mut rng);
28+
let mut out = String::with_capacity(bits / 4 + 1);
29+
group.bench_with_input(BenchmarkId::from_parameter(bits), &a, |bencher, ta| {
30+
bencher.iter(|| {
31+
out.clear();
32+
write!(&mut out, "{:x}", &ta).unwrap();
33+
out.len()
34+
})
35+
});
36+
}
37+
38+
group.finish();
39+
}
40+
41+
fn ubig_to_dec(criterion: &mut Criterion) {
42+
let mut rng = StdRng::seed_from_u64(SEED);
43+
let mut group = criterion.benchmark_group("ubig_to_dec");
44+
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
45+
46+
for log_bits in 1..=6 {
47+
let bits = 10usize.pow(log_bits);
48+
let a = random_ubig(bits, &mut rng);
49+
let mut out = String::with_capacity(bits / 3 + 1);
50+
group.bench_with_input(BenchmarkId::from_parameter(bits), &a, |bencher, ta| {
51+
bencher.iter(|| {
52+
out.clear();
53+
write!(&mut out, "{}", &ta).unwrap();
54+
out.len()
55+
})
56+
});
57+
}
58+
59+
group.finish();
60+
}
61+
62+
fn ubig_from_hex(criterion: &mut Criterion) {
63+
let mut rng = StdRng::seed_from_u64(SEED);
64+
let mut group = criterion.benchmark_group("ubig_from_hex");
65+
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
66+
67+
for log_bits in 1..=6 {
68+
let bits = 10usize.pow(log_bits);
69+
let a = random_ubig(bits, &mut rng);
70+
let s = a.in_radix(16).to_string();
71+
group.bench_with_input(BenchmarkId::from_parameter(bits), &s, |bencher, ts| {
72+
bencher.iter(|| UBig::from_str_radix(ts, 16))
73+
});
74+
}
75+
76+
group.finish();
77+
}
78+
79+
fn ubig_from_dec(criterion: &mut Criterion) {
80+
let mut rng = StdRng::seed_from_u64(SEED);
81+
let mut group = criterion.benchmark_group("ubig_from_dec");
82+
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
83+
84+
for log_bits in 1..=6 {
85+
let bits = 10usize.pow(log_bits);
86+
let a = random_ubig(bits, &mut rng);
87+
let s = a.in_radix(10).to_string();
88+
group.bench_with_input(BenchmarkId::from_parameter(bits), &s, |bencher, ts| {
89+
bencher.iter(|| UBig::from_str_radix(ts, 10))
90+
});
91+
}
92+
93+
group.finish();
94+
}
95+
96+
criterion_group!(benches, ubig_to_hex, ubig_to_dec, ubig_from_hex, ubig_from_dec,);
97+
98+
criterion_main!(benches);

integer/benches/primitive.rs

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use dashu_int::{
1212
UBig,
1313
};
1414
use rand_v08::prelude::*;
15-
use std::fmt::Write;
1615
use std::ops::*;
1716

1817
const SEED: u64 = 1;
@@ -54,82 +53,6 @@ add_binop_benchmark!(ubig_div, div, 6);
5453
add_binop_benchmark!(ubig_gcd, gcd, 6);
5554
add_binop_benchmark!(ubig_gcd_ext, gcd_ext, 5);
5655

57-
fn ubig_to_hex(criterion: &mut Criterion) {
58-
let mut rng = StdRng::seed_from_u64(SEED);
59-
let mut group = criterion.benchmark_group("ubig_to_hex");
60-
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
61-
62-
for log_bits in 1..=6 {
63-
let bits = 10usize.pow(log_bits);
64-
let a = random_ubig(bits, &mut rng);
65-
let mut out = String::with_capacity(bits / 4 + 1);
66-
group.bench_with_input(BenchmarkId::from_parameter(bits), &a, |bencher, ta| {
67-
bencher.iter(|| {
68-
out.clear();
69-
write!(&mut out, "{:x}", &ta).unwrap();
70-
out.len()
71-
})
72-
});
73-
}
74-
75-
group.finish();
76-
}
77-
78-
fn ubig_to_dec(criterion: &mut Criterion) {
79-
let mut rng = StdRng::seed_from_u64(SEED);
80-
let mut group = criterion.benchmark_group("ubig_to_dec");
81-
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
82-
83-
for log_bits in 1..=6 {
84-
let bits = 10usize.pow(log_bits);
85-
let a = random_ubig(bits, &mut rng);
86-
let mut out = String::with_capacity(bits / 3 + 1);
87-
group.bench_with_input(BenchmarkId::from_parameter(bits), &a, |bencher, ta| {
88-
bencher.iter(|| {
89-
out.clear();
90-
write!(&mut out, "{}", &ta).unwrap();
91-
out.len()
92-
})
93-
});
94-
}
95-
96-
group.finish();
97-
}
98-
99-
fn ubig_from_hex(criterion: &mut Criterion) {
100-
let mut rng = StdRng::seed_from_u64(SEED);
101-
let mut group = criterion.benchmark_group("ubig_from_hex");
102-
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
103-
104-
for log_bits in 1..=6 {
105-
let bits = 10usize.pow(log_bits);
106-
let a = random_ubig(bits, &mut rng);
107-
let s = a.in_radix(16).to_string();
108-
group.bench_with_input(BenchmarkId::from_parameter(bits), &s, |bencher, ts| {
109-
bencher.iter(|| UBig::from_str_radix(ts, 16))
110-
});
111-
}
112-
113-
group.finish();
114-
}
115-
116-
fn ubig_from_dec(criterion: &mut Criterion) {
117-
let mut rng = StdRng::seed_from_u64(SEED);
118-
let mut group = criterion.benchmark_group("ubig_from_dec");
119-
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
120-
121-
for log_bits in 1..=6 {
122-
let bits = 10usize.pow(log_bits);
123-
let a = random_ubig(bits, &mut rng);
124-
let s = a.in_radix(10).to_string();
125-
group.bench_with_input(BenchmarkId::from_parameter(bits), &s, |bencher, ts| {
126-
bencher.iter(|| UBig::from_str_radix(ts, 10))
127-
});
128-
}
129-
130-
group.finish();
131-
}
132-
13356
fn ubig_pow(criterion: &mut Criterion) {
13457
let mut group = criterion.benchmark_group("ubig_pow");
13558
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
@@ -225,10 +148,6 @@ criterion_group!(
225148
ubig_div,
226149
ubig_gcd,
227150
ubig_gcd_ext,
228-
ubig_to_hex,
229-
ubig_to_dec,
230-
ubig_from_hex,
231-
ubig_from_dec,
232151
ubig_pow,
233152
ubig_modulo_mul,
234153
ubig_modulo_pow,

rational/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,8 @@ required-features = ["num-order", "dashu-float", "dashu-int/num-order"]
8282
name = "primitive"
8383
required-features = ["rand"]
8484
harness = false
85+
86+
[[bench]]
87+
name = "io"
88+
required-features = ["rand"]
89+
harness = false

0 commit comments

Comments
 (0)