Skip to content

Instantly share code, notes, and snippets.

@stepancheg
Last active December 16, 2015 08:08
Show Gist options
  • Save stepancheg/5403459 to your computer and use it in GitHub Desktop.
Save stepancheg/5403459 to your computer and use it in GitHub Desktop.
extern mod extra;
use std::comm;
use std::io;
use extra::time;
fn to_us(ts: &time::Timespec) -> u64 {
ts.sec as u64 * 1000000 + ((ts.nsec / 1000) as u64)
}
fn now_us() -> u64 {
to_us(&time::get_time())
}
// execute as RUST_THREADS=1 ./switch
// result is about 2us per switch
fn main() {
let (port1, chan1): (Port<int>, Chan<int>) = comm::stream();
let (port2, chan2): (Port<int>, Chan<int>) = comm::stream();
let steps = 100000;
do spawn || {
let mut i = 0;
while i < steps {
chan2.send(port1.recv());
i += 1;
}
};
let start = now_us();
let mut i = 0;
while i < steps {
chan1.send(i as int);
port2.recv();
i += 1;
}
let d = now_us() - start;
let per_step = d * 1000 / steps / 2;
io::println(per_step.to_str() + "ns");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment