2019-01-11 10:53:47 -06:00
|
|
|
use std::fmt::{self, Write as FmtWrite};
|
2019-12-06 22:18:12 -06:00
|
|
|
use std::io::{self, Write as IoWrite};
|
2023-02-20 21:26:40 -06:00
|
|
|
use test::{black_box, Bencher};
|
2019-01-11 10:53:47 -06:00
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn write_vec_value(bh: &mut Bencher) {
|
|
|
|
bh.iter(|| {
|
|
|
|
let mut mem = Vec::new();
|
|
|
|
for _ in 0..1000 {
|
2023-02-20 21:26:40 -06:00
|
|
|
mem.write_all(black_box("abc").as_bytes()).unwrap();
|
2019-01-11 10:53:47 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn write_vec_ref(bh: &mut Bencher) {
|
|
|
|
bh.iter(|| {
|
|
|
|
let mut mem = Vec::new();
|
|
|
|
let wr = &mut mem as &mut dyn io::Write;
|
|
|
|
for _ in 0..1000 {
|
2023-02-20 21:26:40 -06:00
|
|
|
wr.write_all(black_box("abc").as_bytes()).unwrap();
|
2019-01-11 10:53:47 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn write_vec_macro1(bh: &mut Bencher) {
|
|
|
|
bh.iter(|| {
|
|
|
|
let mut mem = Vec::new();
|
|
|
|
let wr = &mut mem as &mut dyn io::Write;
|
|
|
|
for _ in 0..1000 {
|
2023-02-20 21:26:40 -06:00
|
|
|
write!(wr, "{}", black_box("abc")).unwrap();
|
2019-01-11 10:53:47 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn write_vec_macro2(bh: &mut Bencher) {
|
|
|
|
bh.iter(|| {
|
|
|
|
let mut mem = Vec::new();
|
|
|
|
let wr = &mut mem as &mut dyn io::Write;
|
|
|
|
for _ in 0..1000 {
|
2023-02-20 21:26:40 -06:00
|
|
|
write!(wr, "{}", black_box("abc")).unwrap();
|
2019-01-11 10:53:47 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn write_vec_macro_debug(bh: &mut Bencher) {
|
|
|
|
bh.iter(|| {
|
|
|
|
let mut mem = Vec::new();
|
|
|
|
let wr = &mut mem as &mut dyn io::Write;
|
|
|
|
for _ in 0..1000 {
|
2023-02-20 21:26:40 -06:00
|
|
|
write!(wr, "{:?}", black_box("☃")).unwrap();
|
2019-01-11 10:53:47 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn write_str_value(bh: &mut Bencher) {
|
|
|
|
bh.iter(|| {
|
|
|
|
let mut mem = String::new();
|
|
|
|
for _ in 0..1000 {
|
2023-02-20 21:26:40 -06:00
|
|
|
mem.write_str(black_box("abc")).unwrap();
|
2019-01-11 10:53:47 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn write_str_ref(bh: &mut Bencher) {
|
|
|
|
bh.iter(|| {
|
|
|
|
let mut mem = String::new();
|
|
|
|
let wr = &mut mem as &mut dyn fmt::Write;
|
|
|
|
for _ in 0..1000 {
|
2023-02-20 21:26:40 -06:00
|
|
|
wr.write_str(black_box("abc")).unwrap();
|
2019-01-11 10:53:47 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn write_str_macro1(bh: &mut Bencher) {
|
|
|
|
bh.iter(|| {
|
|
|
|
let mut mem = String::new();
|
|
|
|
for _ in 0..1000 {
|
2023-02-20 21:26:40 -06:00
|
|
|
write!(mem, "{}", black_box("abc")).unwrap();
|
2019-01-11 10:53:47 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn write_str_macro2(bh: &mut Bencher) {
|
|
|
|
bh.iter(|| {
|
|
|
|
let mut mem = String::new();
|
|
|
|
let wr = &mut mem as &mut dyn fmt::Write;
|
|
|
|
for _ in 0..1000 {
|
2023-02-20 21:26:40 -06:00
|
|
|
write!(wr, "{}", black_box("abc")).unwrap();
|
2019-01-11 10:53:47 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn write_str_macro_debug(bh: &mut Bencher) {
|
|
|
|
bh.iter(|| {
|
|
|
|
let mut mem = String::new();
|
|
|
|
let wr = &mut mem as &mut dyn fmt::Write;
|
|
|
|
for _ in 0..1000 {
|
2023-02-20 21:26:40 -06:00
|
|
|
write!(wr, "{:?}", black_box("☃")).unwrap();
|
2019-01-11 10:53:47 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-08-28 02:11:44 -05:00
|
|
|
|
2022-05-29 12:21:57 -05:00
|
|
|
#[bench]
|
|
|
|
fn write_str_macro_debug_ascii(bh: &mut Bencher) {
|
|
|
|
bh.iter(|| {
|
|
|
|
let mut mem = String::new();
|
|
|
|
let wr = &mut mem as &mut dyn fmt::Write;
|
|
|
|
for _ in 0..1000 {
|
2023-02-20 21:26:40 -06:00
|
|
|
write!(wr, "{:?}", black_box("Hello, World!")).unwrap();
|
2022-05-29 12:21:57 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-28 02:11:44 -05:00
|
|
|
#[bench]
|
|
|
|
fn write_u128_max(bh: &mut Bencher) {
|
|
|
|
bh.iter(|| {
|
2021-04-15 04:19:24 -05:00
|
|
|
test::black_box(format!("{}", u128::MAX));
|
2020-08-28 02:11:44 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn write_u128_min(bh: &mut Bencher) {
|
|
|
|
bh.iter(|| {
|
|
|
|
let s = format!("{}", 0u128);
|
2021-04-15 04:19:24 -05:00
|
|
|
test::black_box(s);
|
2020-08-28 02:11:44 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn write_u64_max(bh: &mut Bencher) {
|
|
|
|
bh.iter(|| {
|
2021-04-15 04:19:24 -05:00
|
|
|
test::black_box(format!("{}", u64::MAX));
|
2020-08-28 02:11:44 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn write_u64_min(bh: &mut Bencher) {
|
|
|
|
bh.iter(|| {
|
2021-04-15 04:19:24 -05:00
|
|
|
test::black_box(format!("{}", 0u64));
|
2020-08-28 02:11:44 -05:00
|
|
|
});
|
|
|
|
}
|