Debian Codegraph @

debian/rust-vendor/uutils_term_grid/benches/bench-grid.rs

raw
1 // For the full copyright and license information, please view the LICENSE
2 // file that was distributed with this source code.
3
4 use std::hint::black_box;
5
6 use divan::Bencher;
7 use term_grid::{Direction, Filling, Grid, GridOptions};
8
9 const CELL_COUNTS: [usize; 4] = [10, 100, 1_000, 10_000];
10
11 fn generate_cells(n: usize) -> Vec<String> {
12 (0..n).map(|i| format!("file-{i:04}.txt")).collect()
13 }
14
15 #[divan::bench(args = CELL_COUNTS)]
16 fn grid_top_to_bottom(bencher: Bencher, n: usize) {
17 let cells = generate_cells(n);
18 bencher.with_inputs(|| cells.clone()).bench_values(|cells| {
19 black_box(Grid::new(
20 cells,
21 GridOptions {
22 direction: Direction::TopToBottom,
23 filling: Filling::Spaces(2),
24 width: 80,
25 },
26 ))
27 });
28 }
29
30 #[divan::bench(args = CELL_COUNTS)]
31 fn grid_left_to_right(bencher: Bencher, n: usize) {
32 let cells = generate_cells(n);
33 bencher.with_inputs(|| cells.clone()).bench_values(|cells| {
34 black_box(Grid::new(
35 cells,
36 GridOptions {
37 direction: Direction::LeftToRight,
38 filling: Filling::Spaces(2),
39 width: 80,
40 },
41 ))
42 });
43 }
44
45 #[divan::bench(args = CELL_COUNTS)]
46 fn grid_display(bencher: Bencher, n: usize) {
47 let cells = generate_cells(n);
48 let grid = Grid::new(
49 cells,
50 GridOptions {
51 direction: Direction::TopToBottom,
52 filling: Filling::Spaces(2),
53 width: 80,
54 },
55 );
56 bencher.bench(|| black_box(grid.to_string()));
57 }
58
59 #[divan::bench(args = [20, 80, 160, 320])]
60 fn grid_varying_width(bencher: Bencher, width: usize) {
61 let cells = generate_cells(500);
62 bencher.with_inputs(|| cells.clone()).bench_values(|cells| {
63 black_box(Grid::new(
64 cells,
65 GridOptions {
66 direction: Direction::TopToBottom,
67 filling: Filling::Spaces(2),
68 width,
69 },
70 ))
71 });
72 }
73
74 fn main() {
75 divan::main();
76 }