Last active
April 12, 2018 18:27
-
-
Save benaryorg/c90d1771506cdb571dc0323a70007273 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(®ex_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(®ex_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