Skip to content

Instantly share code, notes, and snippets.

@benaryorg
Last active April 12, 2018 18:27
Show Gist options
  • Save benaryorg/c90d1771506cdb571dc0323a70007273 to your computer and use it in GitHub Desktop.
Save benaryorg/c90d1771506cdb571dc0323a70007273 to your computer and use it in GitHub Desktop.
extern crate regex;
use std::env;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::time::Instant;
use regex::Regex;
fn main()
{
let regex_back = Regex::new(r"\w{3,}[.!?]$").unwrap();
let regex_front = Regex::new(r"^[.!?]\w{3}").unwrap();
let dir = env::current_dir().expect("no current dir");
let parent = dir.parent().expect("no parrent");
let path = parent.join("long_ipsum.txt");
assert!(path.is_file());
let file = File::open(path).expect("cannot open file");
let metadata = file.metadata().expect("cannot get metadata");
let mut data = String::with_capacity(metadata.len() as usize);
let reader = BufReader::new(file);
for line in reader.lines()
{
data.push_str(&line.expect("cannot read line"));
}
{
let timer = Instant::now();
let count = data
.lines()
.filter(|str| str.find(&regex_back).is_some())
.count();
let delta = timer.elapsed();
println!("count: {}",count);
println!("delta: {}",delta.as_secs());
}
{
let timer = Instant::now();
let count = data
.lines()
.filter(|str| str.rfind(&regex_front).is_some())
.count();
let delta = timer.elapsed();
println!("count: {}",count);
println!("delta: {}",delta.as_secs());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment