Last active
June 4, 2017 15:42
-
-
Save mmstick/56e899b2710d9d153ba292e9e9c7498c to your computer and use it in GitHub Desktop.
Compile with `rustc -C opt-level=3 -C target-cpu=native -C lto -C panic=abort split.rs -o splitrs`
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
use std::io::{self, BufRead, BufReader, Write}; | |
use std::time::Instant; | |
fn main() { | |
// Obtain a buffered handle to standard input | |
let stdin = io::stdin(); | |
let mut stdin = BufReader::new(stdin.lock()); | |
// Mark the current time for a later duration comparison. | |
let start = Instant::now(); | |
// Obtain the number of words and characters in each word | |
let (mut count, mut num_words, mut num_chars) = (0, 0, 0); | |
let mut line = Vec::with_capacity(128); | |
while stdin.read_until(b'\n', &mut line).unwrap_or(0) > 0 { | |
for word in line.split(|&x| x == b' ') { | |
num_words += 1; | |
num_chars += word.len(); | |
} | |
line.clear(); | |
count += 1; | |
} | |
// Obtain the duration of the computation. | |
let duration = Instant::now().duration_since(start); | |
let mut stderr = io::stderr(); | |
let _ = writeln!(stderr, "Rust : Saw {} lines ({} words/{} chars) in {}.{} seconds.", | |
count, num_words, num_chars, duration.as_secs(), duration.subsec_nanos()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment