Skip to content
Merged
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
114 changes: 98 additions & 16 deletions benches/benches.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::num::NonZeroUsize;
use std::{io, iter};
use tinytable::write_table;

use tinytable::{write_table, write_table_with_fmt};

#[cfg(tinytable_profile_alloc)]
use divan::AllocProfiler;
Expand All @@ -16,47 +17,128 @@ fn main() {
const COLUMNS: usize = 10;
const COLUMN_WIDTHS: [NonZeroUsize; COLUMNS] = [NonZeroUsize::new(10).unwrap(); COLUMNS];

#[divan::bench(args = [10, 100, 1000, 10000], types = [Vec<u8>, io::Empty])]
fn no_padding<W: io::Write + Default>(rows: usize) {
#[divan::bench(args = [10, 100, 1000, 10000])]
fn no_padding(rows: usize) {
const S: &str = " ";
const ROW: [&str; COLUMNS] = [S; COLUMNS];

let mut output: W = Default::default();
let mut output = io::empty();
write_table(&mut output, iter::repeat_n(ROW, rows), &ROW, &COLUMN_WIDTHS).expect("write_table failed");
}

#[divan::bench(args = [10, 100, 1000, 10000], types = [Vec<u8>, io::Empty])]
fn no_padding_unicode<W: io::Write + Default>(rows: usize) {
#[divan::bench(args = [10, 100, 1000, 10000])]
fn no_padding_unicode(rows: usize) {
const S: &str = "あいうえお";
const ROW: [&str; COLUMNS] = [S; COLUMNS];

let mut output: W = Default::default();
let mut output = io::empty();
write_table(&mut output, iter::repeat_n(ROW, rows), &ROW, &COLUMN_WIDTHS).expect("write_table failed");
}

#[divan::bench(args = [10, 100, 1000, 10000], types = [Vec<u8>, io::Empty])]
fn all_padding<W: io::Write + Default>(rows: usize) {
#[divan::bench(args = [10, 100, 1000, 10000])]
fn all_padding(rows: usize) {
const S: &str = "";
const ROW: [&str; COLUMNS] = [S; COLUMNS];

let mut output: W = Default::default();
let mut output = io::empty();
write_table(&mut output, iter::repeat_n(ROW, rows), &ROW, &COLUMN_WIDTHS).expect("write_table failed");
}

#[divan::bench(args = [10, 100, 1000, 10000], types = [Vec<u8>, io::Empty])]
fn single_padding<W: io::Write + Default>(rows: usize) {
#[divan::bench(args = [10, 100, 1000, 10000])]
fn single_padding(rows: usize) {
const S: &str = " ";
const ROW: [&str; COLUMNS] = [S; COLUMNS];

let mut output: W = Default::default();
let mut output = io::empty();
write_table(&mut output, iter::repeat_n(ROW, rows), &ROW, &COLUMN_WIDTHS).expect("write_table failed");
}

#[cfg(feature = "fallible-iterator")]
#[divan::bench(args = [10, 100, 1000, 10000])]
fn single_padding_fallible(rows: usize) {
use fallible_iterator::FallibleIterator;
use tinytable::write_table_fallible;

const S: &str = " ";
const ROW: [&str; COLUMNS] = [S; COLUMNS];

let mut output = io::empty();
write_table_fallible(
&mut output,
fallible_iterator::repeat::<_, ()>(ROW).take(rows),
&ROW,
&COLUMN_WIDTHS,
)
.expect("write_table failed");
}

#[divan::bench(args = [10, 100, 1000, 10000])]
fn single_padding_unicode(rows: usize) {
const S: &str = "あいうえ";
const ROW: [&str; COLUMNS] = [S; COLUMNS];

let mut output = io::empty();
write_table(&mut output, iter::repeat_n(ROW, rows), &ROW, &COLUMN_WIDTHS).expect("write_table failed");
}

#[divan::bench(args = [10, 100, 1000, 10000], types = [Vec<u8>, io::Empty])]
fn too_long<W: io::Write + Default>(rows: usize) {
#[divan::bench(args = [10, 100, 1000, 10000])]
fn single_padding_fmt_reuse_cell(rows: usize) {
const ROW: &str = " ";
const COLUMN_NAMES: [&str; COLUMNS] = [ROW; COLUMNS];

fn fmt(row: &&str, f: &mut String) -> std::fmt::Result {
use std::fmt::Write;
write!(f, "{}", row)
}

let mut output = io::empty();
write_table_with_fmt(
&mut output,
iter::repeat_n(ROW, rows),
&[fmt; COLUMNS],
&COLUMN_NAMES,
&COLUMN_WIDTHS,
)
.expect("write_table failed");
}

#[divan::bench(args = [10, 100, 1000, 10000])]
fn single_padding_fmt_visitor(rows: usize) {
const S: &str = " ";
const ROW: [&str; COLUMNS] = [S; COLUMNS];

fn fmt(index: usize) -> impl Fn(&[&str; COLUMNS], &mut String) -> std::fmt::Result {
use std::fmt::Write;
move |row: &[&str; COLUMNS], f: &mut String| write!(f, "{}", row[index])
}

let mut output = io::empty();
write_table_with_fmt(
&mut output,
iter::repeat_n(ROW, rows),
&[
fmt(0),
fmt(1),
fmt(2),
fmt(3),
fmt(4),
fmt(5),
fmt(6),
fmt(7),
fmt(8),
fmt(9),
],
&ROW,
&COLUMN_WIDTHS,
)
.expect("write_table failed");
}

#[divan::bench(args = [10, 100, 1000, 10000])]
fn too_long(rows: usize) {
const S: &str = " ";
const ROW: [&str; COLUMNS] = [S; COLUMNS];

let mut output: W = Default::default();
let mut output = io::empty();
write_table(&mut output, iter::repeat_n(ROW, rows), &ROW, &COLUMN_WIDTHS).expect("write_table failed");
}