File: stack.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (88 lines) | stat: -rw-r--r-- 2,704 bytes parent folder | download | duplicates (6)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// pest. The Elegant Parser
// Copyright (c) 2018 DragoČ™ Tiselice
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

use criterion::{criterion_group, criterion_main, Criterion};
use pest::Stack;

fn snapshot_push_restore<T: Clone>(elements: impl Iterator<Item = T> + Clone) {
    let mut stack = Stack::<T>::new();
    for elem in elements {
        stack.snapshot();
        stack.push(elem.clone());
        stack.restore();
        stack.push(elem);
    }
}

fn snapshot_push_clear_snapshot<T: Clone>(elements: impl Iterator<Item = T> + Clone) {
    let mut stack = Stack::<T>::new();
    for elem in elements {
        stack.snapshot();
        stack.push(elem);
        stack.clear_snapshot();
    }
}

fn snapshot_pop_restore<T: Clone>(elements: impl Iterator<Item = T>) {
    let mut stack = Stack::<T>::new();
    for elem in elements {
        stack.push(elem);
    }
    while !stack.is_empty() {
        stack.snapshot();
        stack.pop();
        stack.restore();
        stack.pop();
    }
}

fn snapshot_pop_clear<T: Clone>(elements: impl Iterator<Item = T>) {
    let mut stack = Stack::<T>::new();
    for elem in elements {
        stack.push(elem);
    }
    while !stack.is_empty() {
        stack.snapshot();
        stack.pop();
        stack.clear_snapshot();
    }
}

fn benchmark(b: &mut Criterion) {
    use core::iter::repeat;
    // use criterion::black_box;
    let times = 10000usize;
    let small = 0..times;
    let medium = ("", 0usize, 1usize);
    let medium = repeat(medium).take(times);
    let large = [""; 64];
    let large = repeat(large).take(times);
    macro_rules! test_series {
        ($kind:ident) => {
            b.bench_function(stringify!(push - restore - $kind), |b| {
                b.iter(|| snapshot_push_restore($kind.clone()))
            })
            .bench_function(stringify!(push - clear - $kind), |b| {
                b.iter(|| snapshot_push_clear_snapshot($kind.clone()))
            })
            .bench_function(stringify!(pop - restore - $kind), |b| {
                b.iter(|| snapshot_pop_restore($kind.clone()))
            })
            .bench_function(stringify!(pop - clear - $kind), |b| {
                b.iter(|| snapshot_pop_clear($kind.clone()))
            })
        };
    }
    test_series!(small);
    test_series!(medium);
    test_series!(large);
}

criterion_group!(benchmarks, benchmark);
criterion_main!(benchmarks);