Skip to content

Instantly share code, notes, and snippets.

@hfeeki
Forked from lilyball/task_timer.rs
Created January 15, 2014 12:36
Show Gist options
  • Save hfeeki/8435442 to your computer and use it in GitHub Desktop.
Save hfeeki/8435442 to your computer and use it in GitHub Desktop.
/*
* Copyright (C) 2012, xp@renzhi.ca
* All rights reserved.
*
* Ported to rust 0.4 by kevin@sb.org
*/
extern mod std;
use task::*;
fn main() {
let args = os::args();
let numThreads = if vec::len(args) == 2u {
uint::from_str(args[1]).get()
} else {
10u
};
io::println(fmt!("Number of threads: %u", numThreads));
let mut task_chs = ~[];
let mut i = 0u;
let t1 = std::time::precise_time_ns();
while i < numThreads {
let task_ch = do task::spawn_listener::<int> |port, copy i| {
wait_4_cmd(i, port);
};
task_chs += [task_ch];
i += 1u;
}
for task_chs.each |task_ch| {
task_ch.send(-1);
}
let t2 = std::time::precise_time_ns();
let elapsed = t2 - t1;
let a = (elapsed as float) / (numThreads as float) / 1000.0;
io::println(fmt!("Total time: %f ms", ((elapsed as float) / 1000.0 / 1000.0)));
io::println(fmt!("Average : %f microseconds", a));
}
fn wait_4_cmd(_id: uint, p: comm::Port<int>) {
let mut res: int;
loop {
res = p.recv();
//io::println(fmt!("Task %u received: %d", id, res));
if res == -1 { break; }
}
//io::println(fmt!("Task %u done", id));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment