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
19 changes: 10 additions & 9 deletions src/libregex/test/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use std::rand::{Rng, task_rng};
use stdtest::Bencher;
use std::iter::repeat;

use regex::{Regex, NoExpand};

Expand All @@ -22,38 +23,38 @@ fn bench_assert_match(b: &mut Bencher, re: Regex, text: &str) {
fn no_exponential(b: &mut Bencher) {
let n = 100;
let re = Regex::new(format!("{}{}",
"a?".repeat(n),
"a".repeat(n)).as_slice()).unwrap();
let text = "a".repeat(n);
repeat("a?").take(n).collect::<String>(),
repeat("a").take(n).collect::<String>()).as_slice()).unwrap();
let text = repeat("a").take(n).collect::<String>();
bench_assert_match(b, re, text.as_slice());
}

#[bench]
fn literal(b: &mut Bencher) {
let re = regex!("y");
let text = format!("{}y", "x".repeat(50));
let text = format!("{}y", repeat("x").take(50).collect::<String>());
bench_assert_match(b, re, text.as_slice());
}

#[bench]
fn not_literal(b: &mut Bencher) {
let re = regex!(".y");
let text = format!("{}y", "x".repeat(50));
let text = format!("{}y", repeat("x").take(50).collect::<String>());
bench_assert_match(b, re, text.as_slice());
}

#[bench]
fn match_class(b: &mut Bencher) {
let re = regex!("[abcdw]");
let text = format!("{}w", "xxxx".repeat(20));
let text = format!("{}w", repeat("xxxx").take(20).collect::<String>());
bench_assert_match(b, re, text.as_slice());
}

#[bench]
fn match_class_in_range(b: &mut Bencher) {
// 'b' is between 'a' and 'c', so the class range checking doesn't help.
let re = regex!("[ac]");
let text = format!("{}c", "bbbb".repeat(20));
let text = format!("{}c", repeat("bbbb").take(20).collect::<String>());
bench_assert_match(b, re, text.as_slice());
}

Expand All @@ -77,7 +78,7 @@ fn anchored_literal_short_non_match(b: &mut Bencher) {
#[bench]
fn anchored_literal_long_non_match(b: &mut Bencher) {
let re = regex!("^zbc(d|e)");
let text = "abcdefghijklmnopqrstuvwxyz".repeat(15);
let text = repeat("abcdefghijklmnopqrstuvwxyz").take(15).collect::<String>();
b.iter(|| re.is_match(text.as_slice()));
}

Expand All @@ -91,7 +92,7 @@ fn anchored_literal_short_match(b: &mut Bencher) {
#[bench]
fn anchored_literal_long_match(b: &mut Bencher) {
let re = regex!("^.bc(d|e)");
let text = "abcdefghijklmnopqrstuvwxyz".repeat(15);
let text = repeat("abcdefghijklmnopqrstuvwxyz").take(15).collect::<String>();
b.iter(|| re.is_match(text.as_slice()));
}

Expand Down