Sha256: 4a2e0fb093c7f5bbe0fff8689fc0c670c5334344a1bfda376f5faa98a05d459f

Contents?: true

Size: 1.62 KB

Versions: 38

Compression:

Stored size: 1.62 KB

Contents

use std::{str::FromStr, time::Instant};

use regex::Regex;

macro_rules! regex {
    ($re:literal $(,)?) => {{
        static RE: once_cell::sync::OnceCell<regex::Regex> = once_cell::sync::OnceCell::new();
        RE.get_or_init(|| regex::Regex::new($re).unwrap())
    }};
}

fn slow() {
    let s = r##"13.28.24.13 - - [10/Mar/2016:19:29:25 +0100] "GET /etc/lib/pChart2/examples/index.php?Action=View&Script=../../../../cnf/db.php HTTP/1.1" 404 151 "-" "HTTP_Request2/2.2.1 (http://pear.php.net/package/http_request2) PHP/5.3.16""##;

    let mut total = 0;
    for _ in 0..1000 {
        let re = Regex::new(
            r##"^(\S+) (\S+) (\S+) \[([^]]+)\] "([^"]*)" (\d+) (\d+) "([^"]*)" "([^"]*)"$"##,
        )
        .unwrap();
        let size = usize::from_str(re.captures(s).unwrap().get(7).unwrap().as_str()).unwrap();
        total += size;
    }
    println!("{}", total);
}

fn fast() {
    let s = r##"13.28.24.13 - - [10/Mar/2016:19:29:25 +0100] "GET /etc/lib/pChart2/examples/index.php?Action=View&Script=../../../../cnf/db.php HTTP/1.1" 404 151 "-" "HTTP_Request2/2.2.1 (http://pear.php.net/package/http_request2) PHP/5.3.16""##;

    let mut total = 0;
    for _ in 0..1000 {
        let re: &Regex = regex!(
            r##"^(\S+) (\S+) (\S+) \[([^]]+)\] "([^"]*)" (\d+) (\d+) "([^"]*)" "([^"]*)"$"##,
        );
        let size = usize::from_str(re.captures(s).unwrap().get(7).unwrap().as_str()).unwrap();
        total += size;
    }
    println!("{}", total);
}

fn main() {
    let t = Instant::now();
    slow();
    println!("slow: {:?}", t.elapsed());

    let t = Instant::now();
    fast();
    println!("fast: {:?}", t.elapsed());
}

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
wasmtime-14.0.3 ./ext/cargo-vendor/once_cell-1.18.0/examples/regex.rs
wasmtime-14.0.1 ./ext/cargo-vendor/once_cell-1.18.0/examples/regex.rs
wasmtime-14.0.0 ./ext/cargo-vendor/once_cell-1.18.0/examples/regex.rs
wasmtime-13.0.0 ./ext/cargo-vendor/once_cell-1.18.0/examples/regex.rs
wasmtime-12.0.1 ./ext/cargo-vendor/once_cell-1.18.0/examples/regex.rs
wasmtime-12.0.0 ./ext/cargo-vendor/once_cell-1.18.0/examples/regex.rs
wasmtime-11.0.0 ./ext/cargo-vendor/once_cell-1.18.0/examples/regex.rs
wasmtime-10.0.1 ./ext/cargo-vendor/once_cell-1.18.0/examples/regex.rs
wasmtime-10.0.0 ./ext/cargo-vendor/once_cell-1.18.0/examples/regex.rs
wasmtime-9.0.4 ./ext/cargo-vendor/once_cell-1.18.0/examples/regex.rs
wasmtime-9.0.1 ./ext/cargo-vendor/once_cell-1.17.1/examples/regex.rs
wasmtime-8.0.0 ./ext/cargo-vendor/once_cell-1.17.1/examples/regex.rs
wasmtime-7.0.0 ./ext/cargo-vendor/once_cell-1.17.1/examples/regex.rs
wasmtime-6.0.1 ./ext/cargo-vendor/once_cell-1.17.1/examples/regex.rs
wasmtime-6.0.0 ./ext/cargo-vendor/once_cell-1.17.1/examples/regex.rs
wasmtime-5.0.0 ./ext/cargo-vendor/once_cell-1.17.0/examples/regex.rs
wasmtime-0.4.1 ./ext/cargo-vendor/once_cell-1.16.0/examples/regex.rs
wasmtime-0.4.0 ./ext/cargo-vendor/once_cell-1.16.0/examples/regex.rs