update str.contains benchmarks

This commit is contained in:
The 8472 2022-10-30 21:50:49 +01:00
parent 4844e5162c
commit 467b299e53
1 changed files with 54 additions and 3 deletions

View File

@ -1,3 +1,4 @@
use core::iter::Iterator;
use test::{black_box, Bencher};
#[bench]
@ -122,14 +123,13 @@ fn bench_contains_short_short(b: &mut Bencher) {
let haystack = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
let needle = "sit";
b.bytes = haystack.len() as u64;
b.iter(|| {
assert!(black_box(haystack).contains(black_box(needle)));
})
}
#[bench]
fn bench_contains_short_long(b: &mut Bencher) {
let haystack = "\
static LONG_HAYSTACK: &str = "\
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse quis lorem sit amet dolor \
ultricies condimentum. Praesent iaculis purus elit, ac malesuada quam malesuada in. Duis sed orci \
eros. Suspendisse sit amet magna mollis, mollis nunc luctus, imperdiet mi. Integer fringilla non \
@ -164,8 +164,46 @@ feugiat. Etiam quis mauris vel risus luctus mattis a a nunc. Nullam orci quam, i
vehicula in, porttitor ut nibh. Duis sagittis adipiscing nisl vitae congue. Donec mollis risus eu \
leo suscipit, varius porttitor nulla porta. Pellentesque ut sem nec nisi euismod vehicula. Nulla \
malesuada sollicitudin quam eu fermentum.";
#[bench]
fn bench_contains_2b_repeated_long(b: &mut Bencher) {
let haystack = LONG_HAYSTACK;
let needle = "::";
b.bytes = haystack.len() as u64;
b.iter(|| {
assert!(!black_box(haystack).contains(black_box(needle)));
})
}
#[bench]
fn bench_contains_short_long(b: &mut Bencher) {
let haystack = LONG_HAYSTACK;
let needle = "english";
b.bytes = haystack.len() as u64;
b.iter(|| {
assert!(!black_box(haystack).contains(black_box(needle)));
})
}
#[bench]
fn bench_contains_16b_in_long(b: &mut Bencher) {
let haystack = LONG_HAYSTACK;
let needle = "english language";
b.bytes = haystack.len() as u64;
b.iter(|| {
assert!(!black_box(haystack).contains(black_box(needle)));
})
}
#[bench]
fn bench_contains_32b_in_long(b: &mut Bencher) {
let haystack = LONG_HAYSTACK;
let needle = "the english language sample text";
b.bytes = haystack.len() as u64;
b.iter(|| {
assert!(!black_box(haystack).contains(black_box(needle)));
})
@ -176,6 +214,18 @@ fn bench_contains_bad_naive(b: &mut Bencher) {
let haystack = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
let needle = "aaaaaaaab";
b.bytes = haystack.len() as u64;
b.iter(|| {
assert!(!black_box(haystack).contains(black_box(needle)));
})
}
#[bench]
fn bench_contains_bad_simd(b: &mut Bencher) {
let haystack = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
let needle = "aaabaaaa";
b.bytes = haystack.len() as u64;
b.iter(|| {
assert!(!black_box(haystack).contains(black_box(needle)));
})
@ -186,6 +236,7 @@ fn bench_contains_equal(b: &mut Bencher) {
let haystack = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
let needle = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
b.bytes = haystack.len() as u64;
b.iter(|| {
assert!(black_box(haystack).contains(black_box(needle)));
})