Created
January 19, 2014 11:23
-
-
Save magistere/8503551 to your computer and use it in GitHub Desktop.
Port of https://gist.github.com/kballard/3915251 to Rust 0.9
This file contains 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 mod extra; | |
use std::os; | |
use std::from_str::FromStr; | |
use extra::time; | |
fn main() { | |
let args = os::args(); | |
let numThreads = if args.len() == 2 { | |
FromStr::from_str(args[1]).unwrap() | |
} else { | |
10 | |
}; | |
println!("Number of threads: {}", numThreads); | |
let mut chans = ~[]; | |
let t1 = time::precise_time_ns(); | |
for i in range(0, numThreads) { | |
let (port, chan) = Chan::new(); | |
chans.push(chan); | |
do spawn { | |
thread(i, port); | |
} | |
} | |
for chan in chans.iter() { | |
chan.send(-1); | |
} | |
let t2 = time::precise_time_ns(); | |
let elapsed = t2 - t1; | |
println!("Total time: {} ms", 1e-6 * elapsed as f64); | |
println!("Average : {} us", 1e-3 * elapsed as f64 / numThreads as f64); | |
} | |
fn thread(_: int, p: Port<int>) { | |
let mut res: int; | |
loop { | |
res = p.recv(); | |
if res == -1 { break; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment