Skip to content

Instantly share code, notes, and snippets.

@4JX
Created January 28, 2022 17:19
Show Gist options
  • Save 4JX/f9ed141f337b5a8127eb416077d04b35 to your computer and use it in GitHub Desktop.
Save 4JX/f9ed141f337b5a8127eb416077d04b35 to your computer and use it in GitHub Desktop.
use ferinth::Ferinth;
use lazy_regex::{regex, regex_is_match};
use lazy_static::lazy_static;
use std::time::Instant;
const LOOP_NUM: i32 = 100000;
#[tokio::main]
async fn main() {
//This is just to avoid the compiler from optimizing the code
let fer = Ferinth::new("Perf test");
let sodium_ver = fer.list_versions("AANobbMI").await.unwrap();
let sha1_opt = sodium_ver[0].files[0].hashes.sha1.clone();
let sha1 = sha1_opt.unwrap();
manual_regex(sha1.as_str());
lazy_static(sha1.as_str());
lazy_regex(sha1.as_str());
lazy_is_match(sha1.as_str());
}
fn manual_regex(sha1: &str) {
let mut bool = false;
let now_regex = Instant::now();
for _ in 0..LOOP_NUM {
bool = regex::Regex::new("^[a-f0-9]{40}$").unwrap().is_match(sha1);
}
println!("Manual regex: {:?} {}", now_regex.elapsed(), bool);
}
fn lazy_static(sha1: &str) {
let mut bool = false;
let now_regex = Instant::now();
for _ in 0..LOOP_NUM {
lazy_static! {
static ref RE: regex::Regex = regex::Regex::new("^[a-f0-9]{40}$").unwrap();
};
bool = RE.is_match(sha1);
}
println!("Lazy static: {:?} {}", now_regex.elapsed(), bool);
}
fn lazy_regex(sha1: &str) {
let mut bool = false;
let now_regex = Instant::now();
for _ in 0..LOOP_NUM {
let r = regex!("^[a-f0-9]{40}$");
bool = r.is_match(sha1);
}
println!("Lazy regex: {:?} {}", now_regex.elapsed(), bool);
}
fn lazy_is_match(sha1: &str) {
let mut bool = false;
let now_regex = Instant::now();
for _ in 0..LOOP_NUM {
bool = regex_is_match!("^[a-f0-9]{40}$", sha1);
}
println!("Lazy is match: {:?} {}", now_regex.elapsed(), bool);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment