Skip to content

Instantly share code, notes, and snippets.

@arjantop
Created June 6, 2014 10:07
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 arjantop/2d341b9649167f03a394 to your computer and use it in GitHub Desktop.
Save arjantop/2d341b9649167f03a394 to your computer and use it in GitHub Desktop.
running 3 tests
test with_barriers ... bench: 101409571 ns/iter (+/- 16367483)
test with_channels ... bench: 37843442 ns/iter (+/- 13011404)
test with_semaphores ... bench: 10861761 ns/iter (+/- 4595890)
test result: ok. 0 passed; 0 failed; 0 ignored; 3 measured
extern crate sync;
extern crate test;
use sync::{Semaphore, Barrier, Arc};
use test::Bencher;
static TASK_COUNT: int = 1000;
#[bench]
fn with_channels(b: &mut Bencher) {
b.iter(|| {
let (tx, rx) = channel();
for _ in range(0, TASK_COUNT) {
let task_tx = tx.clone();
spawn(proc() {
task_tx.send(());
});
}
for _ in range(0, TASK_COUNT) {
rx.recv();
}
});
}
#[bench]
fn with_semaphores(b: &mut Bencher) {
b.iter(|| {
let semaphore = Arc::new(Semaphore::new(0));
for _ in range(0, TASK_COUNT) {
let task_semaphore = semaphore.clone();
spawn(proc() {
task_semaphore.release();
});
}
for _ in range(0, TASK_COUNT) {
semaphore.acquire();
}
});
}
#[bench]
fn with_barriers(b: &mut Bencher) {
b.iter(|| {
let barrier = Arc::new(Barrier::new(TASK_COUNT as uint + 1));
for _ in range(0, TASK_COUNT) {
let task_barrier = barrier.clone();
spawn(proc() {
task_barrier.wait();
});
}
barrier.wait();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment