Skip to content

Instantly share code, notes, and snippets.

@renato-zannon
Created June 5, 2014 22:11
Show Gist options
  • Save renato-zannon/ea5208c0462fb8f7ed18 to your computer and use it in GitHub Desktop.
Save renato-zannon/ea5208c0462fb8f7ed18 to your computer and use it in GitHub Desktop.
Rust: Semaphores vs channels
running 2 tests
test with_channels ... bench: 876410 ns/iter (+/- 655463)
test with_semaphores ... bench: 513476 ns/iter (+/- 711503)
test result: ok. 0 passed; 0 failed; 0 ignored; 2 measured
extern crate sync;
extern crate test;
use sync::{Semaphore, Arc};
use test::Bencher;
static TASK_COUNT: int = 10;
#[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 sem = Arc::new(Semaphore::new(TASK_COUNT));
for _ in range(0, TASK_COUNT) {
let task_sem = sem.clone();
task_sem.acquire();
spawn(proc() {
task_sem.release();
});
}
for _ in range(0, TASK_COUNT) {
sem.acquire();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment