-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbench.rs
More file actions
42 lines (38 loc) · 1.25 KB
/
bench.rs
File metadata and controls
42 lines (38 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use criterion::{criterion_group, criterion_main, Criterion};
use std::fmt::Display;
use std::hint;
use std::io::Write;
use std::{f32, f64};
fn do_bench(c: &mut Criterion, group_name: &str, float: impl ryu_js::Float + Display) {
let mut group = c.benchmark_group(group_name);
group.bench_function("ryu_js", |b| {
let mut buf = ryu_js::Buffer::new();
b.iter(move || {
let float = hint::black_box(float);
let formatted = buf.format_finite(float);
hint::black_box(formatted);
});
});
group.bench_function("std::fmt", |b| {
let mut buf = Vec::with_capacity(20);
b.iter(|| {
buf.clear();
let float = hint::black_box(float);
write!(&mut buf, "{float}").unwrap();
hint::black_box(buf.as_slice());
});
});
group.finish();
}
fn bench(c: &mut Criterion) {
do_bench(c, "f64[0]", 0f64);
do_bench(c, "f64[short]", 0.1234f64);
do_bench(c, "f64[e]", f64::consts::E);
do_bench(c, "f64[max]", f64::MAX);
do_bench(c, "f32[0]", 0f32);
do_bench(c, "f32[short]", 0.1234f32);
do_bench(c, "f32[e]", f32::consts::E);
do_bench(c, "f32[max]", f32::MAX);
}
criterion_group!(benches, bench);
criterion_main!(benches);