| 1 |
#![allow(clippy::unreadable_literal)] |
| 2 |
|
| 3 |
use criterion::{criterion_group, criterion_main, Criterion}; |
| 4 |
use std::f64; |
| 5 |
use std::hint; |
| 6 |
use std::io::Write; |
| 7 |
|
| 8 |
fn do_bench(c: &mut Criterion, group_name: &str, float: f64) { |
| 9 |
let mut group = c.benchmark_group(group_name); |
| 10 |
group.bench_function("zmij", |b| { |
| 11 |
let mut buf = zmij::Buffer::new(); |
| 12 |
b.iter(move || { |
| 13 |
let float = hint::black_box(float); |
| 14 |
let formatted = buf.format_finite(float); |
| 15 |
hint::black_box(formatted); |
| 16 |
}); |
| 17 |
}); |
| 18 |
group.bench_function("ryu", |b| { |
| 19 |
let mut buf = ryu::Buffer::new(); |
| 20 |
b.iter(move || { |
| 21 |
let float = hint::black_box(float); |
| 22 |
let formatted = buf.format_finite(float); |
| 23 |
hint::black_box(formatted); |
| 24 |
}); |
| 25 |
}); |
| 26 |
group.bench_function("std::fmt", |b| { |
| 27 |
let mut buf = Vec::with_capacity(20); |
| 28 |
b.iter(|| { |
| 29 |
buf.clear(); |
| 30 |
let float = hint::black_box(float); |
| 31 |
write!(&mut buf, "{float}").unwrap(); |
| 32 |
hint::black_box(buf.as_slice()); |
| 33 |
}); |
| 34 |
}); |
| 35 |
group.finish(); |
| 36 |
} |
| 37 |
|
| 38 |
fn bench(c: &mut Criterion) { |
| 39 |
do_bench(c, "f64[0]", 0f64); |
| 40 |
do_bench(c, "f64[short]", 0.1234f64); |
| 41 |
do_bench(c, "f64[medium]", 0.123456789f64); |
| 42 |
do_bench(c, "f64[e]", f64::consts::E); |
| 43 |
do_bench(c, "f64[max]", f64::MAX); |
| 44 |
} |
| 45 |
|
| 46 |
criterion_group!(benches, bench); |
| 47 |
criterion_main!(benches); |