Skip to content

Instantly share code, notes, and snippets.

@magistere
Created January 19, 2014 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magistere/8503551 to your computer and use it in GitHub Desktop.
Save magistere/8503551 to your computer and use it in GitHub Desktop.
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