1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10 #![allow(non_snake_case_functions)]
12 use std::rand::{Rng, task_rng};
15 use regex::{Regex, NoExpand};
17 fn bench_assert_match(b: &mut Bencher, re: Regex, text: &str) {
18 b.iter(|| if !re.is_match(text) { fail!("no match") });
22 fn no_exponential(b: &mut Bencher) {
24 let re = Regex::new(format!("{}{}",
26 "a".repeat(n)).as_slice()).unwrap();
27 let text = "a".repeat(n);
28 bench_assert_match(b, re, text.as_slice());
32 fn literal(b: &mut Bencher) {
34 let text = format!("{}y", "x".repeat(50));
35 bench_assert_match(b, re, text.as_slice());
39 fn not_literal(b: &mut Bencher) {
40 let re = regex!(".y");
41 let text = format!("{}y", "x".repeat(50));
42 bench_assert_match(b, re, text.as_slice());
46 fn match_class(b: &mut Bencher) {
47 let re = regex!("[abcdw]");
48 let text = format!("{}w", "xxxx".repeat(20));
49 bench_assert_match(b, re, text.as_slice());
53 fn match_class_in_range(b: &mut Bencher) {
54 // 'b' is between 'a' and 'c', so the class range checking doesn't help.
55 let re = regex!("[ac]");
56 let text = format!("{}c", "bbbb".repeat(20));
57 bench_assert_match(b, re, text.as_slice());
61 fn replace_all(b: &mut Bencher) {
62 let re = regex!("[cjrw]");
63 let text = "abcdefghijklmnopqrstuvwxyz";
64 // FIXME: This isn't using the $name expand stuff.
65 // It's possible RE2/Go is using it, but currently, the expand in this
66 // crate is actually compiling a regex, so it's incredibly slow.
67 b.iter(|| re.replace_all(text, NoExpand("")));
71 fn anchored_literal_short_non_match(b: &mut Bencher) {
72 let re = regex!("^zbc(d|e)");
73 let text = "abcdefghijklmnopqrstuvwxyz";
74 b.iter(|| re.is_match(text));
78 fn anchored_literal_long_non_match(b: &mut Bencher) {
79 let re = regex!("^zbc(d|e)");
80 let text = "abcdefghijklmnopqrstuvwxyz".repeat(15);
81 b.iter(|| re.is_match(text.as_slice()));
85 fn anchored_literal_short_match(b: &mut Bencher) {
86 let re = regex!("^.bc(d|e)");
87 let text = "abcdefghijklmnopqrstuvwxyz";
88 b.iter(|| re.is_match(text));
92 fn anchored_literal_long_match(b: &mut Bencher) {
93 let re = regex!("^.bc(d|e)");
94 let text = "abcdefghijklmnopqrstuvwxyz".repeat(15);
95 b.iter(|| re.is_match(text.as_slice()));
99 fn one_pass_short_a(b: &mut Bencher) {
100 let re = regex!("^.bc(d|e)*$");
101 let text = "abcddddddeeeededd";
102 b.iter(|| re.is_match(text));
106 fn one_pass_short_a_not(b: &mut Bencher) {
107 let re = regex!(".bc(d|e)*$");
108 let text = "abcddddddeeeededd";
109 b.iter(|| re.is_match(text));
113 fn one_pass_short_b(b: &mut Bencher) {
114 let re = regex!("^.bc(?:d|e)*$");
115 let text = "abcddddddeeeededd";
116 b.iter(|| re.is_match(text));
120 fn one_pass_short_b_not(b: &mut Bencher) {
121 let re = regex!(".bc(?:d|e)*$");
122 let text = "abcddddddeeeededd";
123 b.iter(|| re.is_match(text));
127 fn one_pass_long_prefix(b: &mut Bencher) {
128 let re = regex!("^abcdefghijklmnopqrstuvwxyz.*$");
129 let text = "abcdefghijklmnopqrstuvwxyz";
130 b.iter(|| re.is_match(text));
134 fn one_pass_long_prefix_not(b: &mut Bencher) {
135 let re = regex!("^.bcdefghijklmnopqrstuvwxyz.*$");
136 let text = "abcdefghijklmnopqrstuvwxyz";
137 b.iter(|| re.is_match(text));
140 macro_rules! throughput(
141 ($name:ident, $regex:expr, $size:expr) => (
143 fn $name(b: &mut Bencher) {
144 let text = gen_text($size);
146 b.iter(|| if $regex.is_match(text.as_slice()) { fail!("match") });
151 fn easy0() -> Regex { regex!("ABCDEFGHIJKLMNOPQRSTUVWXYZ$") }
152 fn easy1() -> Regex { regex!("A[AB]B[BC]C[CD]D[DE]E[EF]F[FG]G[GH]H[HI]I[IJ]J$") }
153 fn medium() -> Regex { regex!("[XYZ]ABCDEFGHIJKLMNOPQRSTUVWXYZ$") }
154 fn hard() -> Regex { regex!("[ -~]*ABCDEFGHIJKLMNOPQRSTUVWXYZ$") }
156 fn gen_text(n: uint) -> String {
157 let mut rng = task_rng();
158 let mut bytes = rng.gen_ascii_chars().map(|n| n as u8).take(n)
159 .collect::<Vec<u8>>();
160 for (i, b) in bytes.mut_iter().enumerate() {
165 String::from_utf8(bytes).unwrap()
168 throughput!(easy0_32, easy0(), 32)
169 throughput!(easy0_1K, easy0(), 1<<10)
170 throughput!(easy0_32K, easy0(), 32<<10)
172 throughput!(easy1_32, easy1(), 32)
173 throughput!(easy1_1K, easy1(), 1<<10)
174 throughput!(easy1_32K, easy1(), 32<<10)
176 throughput!(medium_32, medium(), 32)
177 throughput!(medium_1K, medium(), 1<<10)
178 throughput!(medium_32K,medium(), 32<<10)
180 throughput!(hard_32, hard(), 32)
181 throughput!(hard_1K, hard(), 1<<10)
182 throughput!(hard_32K,hard(), 32<<10)