File: gstring.rs

package info (click to toggle)
rust-glib 0.20.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,308 kB
  • sloc: makefile: 18
file content (46 lines) | stat: -rw-r--r-- 1,164 bytes parent folder | download | duplicates (4)
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
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use glib::IntoGStr;

pub fn str_into_gstr(c: &mut Criterion) {
    c.bench_function("str as IntoGStr", |b| {
        b.iter(|| {
            "abcdefg".run_with_gstr(|g| {
                black_box(g);
            })
        })
    });
}

pub fn string_into_gstr(c: &mut Criterion) {
    c.bench_function("String as IntoGStr", |b| {
        b.iter(|| {
            let mut s = String::from("abcdefg");
            s.shrink_to_fit();
            assert_eq!(s.capacity(), s.len());
            s.run_with_gstr(|g| {
                black_box(g);
            })
        })
    });
}

pub fn string_with_capacity_into_gstr(c: &mut Criterion) {
    c.bench_function("String::with_capacity as IntoGStr", |b| {
        b.iter(|| {
            let mut s = String::with_capacity(100);
            s.push_str("abcdefg");
            assert!(s.capacity() > s.len());
            s.run_with_gstr(|g| {
                black_box(g);
            })
        })
    });
}

criterion_group!(
    benches,
    str_into_gstr,
    string_into_gstr,
    string_with_capacity_into_gstr
);
criterion_main!(benches);