Improve performance by replacing bit_vec with fixedbitset.

Before:
    test bench_solve_100  ... bench:      53,365 ns/iter (+/- 651)
    test bench_solve_1000 ... bench:   5,864,809 ns/iter (+/- 102,059)
    test bench_solve_2000 ... bench:  34,347,333 ns/iter (+/- 200,639)

After:

    test bench_solve_100  ... bench:      41,574 ns/iter (+/- 765)
    test bench_solve_1000 ... bench:   4,693,452 ns/iter (+/- 189,201)
    test bench_solve_2000 ... bench:  29,954,973 ns/iter (+/- 438,573)
This commit is contained in:
Michael Neumann 2015-11-29 15:03:57 +01:00
parent c61d7152a3
commit 856ea59d70
3 changed files with 9 additions and 9 deletions

View File

@ -8,4 +8,4 @@ description = "Kuhn-Munkres (aka Hungarian) algorithm for solving the Assignment
keywords = ["algorithm"] keywords = ["algorithm"]
[dependencies] [dependencies]
bit-vec = "*" fixedbitset = "0.1"

View File

@ -1,10 +1,10 @@
use bit_vec::BitVec; use fixedbitset::FixedBitSet;
#[derive(Debug)] #[derive(Debug)]
pub struct Coverage { pub struct Coverage {
n: usize, n: usize,
rows: BitVec, rows: FixedBitSet,
cols: BitVec, cols: FixedBitSet,
} }
impl Coverage { impl Coverage {
@ -13,19 +13,19 @@ impl Coverage {
pub fn new(n: usize) -> Coverage { pub fn new(n: usize) -> Coverage {
Coverage { Coverage {
n: n, n: n,
rows: BitVec::from_elem(n, false), rows: FixedBitSet::with_capacity(n),
cols: BitVec::from_elem(n, false), cols: FixedBitSet::with_capacity(n),
} }
} }
#[inline] #[inline]
pub fn is_row_covered(&self, row: usize) -> bool { pub fn is_row_covered(&self, row: usize) -> bool {
self.rows[row] self.rows.contains(row)
} }
#[inline] #[inline]
pub fn is_col_covered(&self, col: usize) -> bool { pub fn is_col_covered(&self, col: usize) -> bool {
self.cols[col] self.cols.contains(col)
} }
#[inline] #[inline]

View File

@ -17,7 +17,7 @@
// * More test cases // * More test cases
// * Non-square matrices // * Non-square matrices
extern crate bit_vec; extern crate fixedbitset;
#[cfg(test)] #[cfg(test)]
extern crate test; extern crate test;