Added benchmarks and fixed a issue with the join method.

This commit is contained in:
Acrimon 2019-06-25 12:59:43 +02:00
parent 4b1356855c
commit 22371f3157
7 changed files with 83 additions and 4 deletions

3
.gitignore vendored
View file

@ -1,3 +1,6 @@
/target
**/*.rs.bk
Cargo.lock
uvth-bench/target
uvth-bench/**/*.rs.bk
uvth-bench/Cargo.lock

View file

@ -1,6 +1,6 @@
[package]
name = "uvth"
version = "1.0.2"
version = "1.0.3"
authors = ["Acrimon <joel.wejdenstal@gmail.com>"]
edition = "2018"
license = "MIT"

View file

@ -1,3 +1,17 @@
# uvth
Compact and efficient threadpool implementation as an alternative to the threadpool crate.
uvth is a more efficient alternative to threadpool with less overhead.
## benchmarks
```
i7-7700HQ 4C/8T Clear Linux
Time taken to run 100k no-op (empty) jobs. Averages from 5050 iterations.
threadpool_crate time: [42.201 ms 42.373 ms 42.560 ms]
threadpool_uvth time: [4.4844 ms 4.5123 ms 4.5412 ms]
```

View file

@ -5,7 +5,17 @@ use crossbeam_channel::{unbounded, Receiver, Sender};
use std::sync::Arc;
use std::thread;
type Task = Box<dyn FnOnce() + Send>;
trait Job: Send {
fn run(self: Box<Self>);
}
impl<F: FnOnce() + Send> Job for F {
fn run(self: Box<Self>) {
(*self)();
}
}
type Task = Box<dyn Job>;
enum Message {
Task(Task),
@ -71,7 +81,7 @@ impl Worker {
debug!("Worker thread started.");
while let Some(message) = self.queue.remove() {
match message {
Message::Task(task) => task(),
Message::Task(task) => task.run(),
Message::Exit => break,
}
}
@ -134,7 +144,7 @@ impl ThreadPool {
/// Wait for all workers to exit.
pub fn join(&self) {
for _ in 0..self.worker_count {
self.notify_exit.recv().unwrap();
let _ = self.notify_exit.recv();
}
}
}

14
uvth-bench/Cargo.toml Normal file
View file

@ -0,0 +1,14 @@
[package]
name = "uvth-bench"
version = "0.1.0"
authors = ["Acrimon <joel.wejdenstal@gmail.com>"]
edition = "2018"
[dependencies]
uvth = { path = ".." }
threadpool = "1.7"
criterion = "0.2"
[[bench]]
name = "threadpool"
harness = false

View file

@ -0,0 +1,35 @@
#[macro_use]
extern crate criterion;
use criterion::Criterion;
const JOBC: usize = 100000;
fn threadpool() {
let pool = threadpool::ThreadPool::new(8);
for _ in 0..JOBC {
pool.execute(|| {
let _ = 8 + 9;
});
}
pool.join();
}
fn uvth_threadpool() {
let pool = uvth::ThreadPool::new(8);
for _ in 0..JOBC {
pool.execute(|| {
let _ = 8 + 9;
});
}
pool.terminate();
pool.join();
}
fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("threadpool_crate", |b| b.iter(|| threadpool()));
c.bench_function("threadpool_uvth", |b| b.iter(|| uvth_threadpool()));
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

3
uvth-bench/src/main.rs Normal file
View file

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}