Skip to content

Instantly share code, notes, and snippets.

@thomcc

thomcc/bench.rs Secret

Created June 10, 2021 16:36
Show Gist options
  • Save thomcc/eeb77d34d8924444b5778d2e451caac6 to your computer and use it in GitHub Desktop.
Save thomcc/eeb77d34d8924444b5778d2e451caac6 to your computer and use it in GitHub Desktop.
#![feature(test)]
// quick and dirty throwaway code, baby
#![allow(warnings)]
extern crate regex;
extern crate test;
use regex::Regex;
use test::{black_box, Bencher};
// `testdata` is a copy of this file:
// https://github.com/BurntSushi/bstr/blob/master/src/ext_slice.rs
// ... because it's big and realistic. (note that I didn't really
// see a perf difference between naive/optim with small inputs)
const TESTDATA: &str = include_str!("testdata");
// these are in separate files because i just couldn't take looking
// at them anymore. the files are attached to the gist. with my luck,
// theythe gist will be in the wrong order too.
const NAIVE: &str = include_str!("rx_naive.txt");
const OPTIM: &str = include_str!("rx_optim.txt");
#[bench]
fn naive(b: &mut Bencher) {
let re = Regex::new(NAIVE.trim()).unwrap();
b.iter(|| {
let mut pos = 0;
while let Some(m) = re.find_at(TESTDATA, pos) {
pos = black_box(m).end();
}
black_box(pos);
});
}
#[bench]
fn quote_optimized_unquote(b: &mut Bencher) {
let re = Regex::new(OPTIM.trim()).unwrap();
b.iter(|| {
let mut pos = 0;
while let Some(m) = re.find_at(TESTDATA, pos) {
pos = black_box(m).end();
}
black_box(pos);
});
}
\b(as|break|const|continue|crate|else|enum|extern|false|fn|for|if|impl|in|let|loop|match|mod|move|mut|pub|ref|return|self|Self|static|struct|super|trait|true|type|unsafe|use|where|while|abstract|become|box|do|final|macro|override|priv|typeof|unsized|virtual|yield|try|i8|i16|i32|i64|i128|isize|u8|u16|u32|u64|u128|usize|bool|char|str|f32|f64)\b
\b(Self|a(?:bstract|s)|b(?:ecome|o(?:ol|x)|reak)|c(?:har|on(?:st|tinue)|rate)|do|e(?:lse|num|xtern)|f(?:32|64|alse|inal|n|or)|i(?:1(?:28|6)|32|64|mpl|size|[8fn])|l(?:et|oop)|m(?:a(?:cro|tch)|o(?:d|ve)|ut)|override|p(?:riv|ub)|re(?:f|turn)|s(?:elf|t(?:atic|r(?:(?:uct)?))|uper)|t(?:r(?:ait|ue|y)|ype(?:(?:of)?))|u(?:1(?:28|6)|32|64|8|ns(?:afe|ized)|s(?:(?:(?:iz)?)e))|virtual|wh(?:(?:er|il)e)|yield)\b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment