Skip to content

Instantly share code, notes, and snippets.

@Dr-Emann
Forked from pnispel/fast.rs
Created Oct 1, 2015
Embed
What would you like to do?
bad rust?
let start = time::now();
let metadata = try!(fs::metadata(&path));
let mut file = try!(File::open(&path));
let mut buf = vec![0u8; metadata.len() as usize];
try!(file.read(&mut buf));
buf.make_ascii_lowercase();
let mut str: &str = try!(str::from_utf8(&buf));
let mut splits: Vec<&str> = str.split(|c| !char::is_alphanumeric(c) && c != '\'' && c != '`').collect();
splits.sort();
let diff = time::now() - start;
let ns = diff.num_nanoseconds().unwrap() as f64;
let ticks = 1000000000.0 as f64;
println!("time: {}", ns / ticks);
// cargo run --release : time: 0.210899
// cargo run : time: 2.885272
import time
start_time = time.time()
words = [x.strip().lower().decode('utf-8').split() for x in open('./text.txt')]
words = [item for sublist in words for item in sublist]
words.sort()
print("--- %s seconds ---" % (time.time() - start_time)) # --- 0.675938129425 seconds ---
let path = String::from("./text.txt");
let start = time::now();
let split_regex = Regex::new(r"[^a-zA-Z0-9’']+").unwrap();
let os_path = Path::new(&path);
let mut file = try!(File::open(&os_path));
let mut s = String::new();
try!(file.read_to_string(&mut s));
let mut splits: Vec<String> = splitRegex.split(&s)
.map(|x| x.to_ascii_lowercase())
.filter(|x| x.len() != 0)
.collect();
splits.sort();
let diff = start - time::now();
let ns = diff.num_nanoseconds().unwrap() as f64;
let ticks = 1000000000.0 as f64;
println!("time: {}", ns / ticks); // time: -14046872000, -14.046872
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment