Last active
October 1, 2015 04:46
-
-
Save pnispel/43257e7f5849df56db9c to your computer and use it in GitHub Desktop.
bad rust?
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
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 |
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
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 --- |
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
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