1个不稳定发布
0.1.0 | 2023年7月28日 |
---|
#2073 在 算法
22KB
300 行
flo_sparse_array
use flo_sparse_array::*;
let mut sparse_array = SparseArray::empty();
sparse_array.insert(12345, "Test");
assert!(sparse_array.get(12345) == "Test");
本crate提供了一个简单稀疏数组类型的实现。这是一种特殊的哈希表,它将 usize 映射到任何数据类型。与正常的Rust HashMap
相比,其主要优势在于读取或更新现有值时的性能。这种性能也非常稳定。
在写入新值时通常也稍微快一些,但由于这两种哈希函数的性能都包含一个随机元素,所以这里的差异可能略有不同。
实现这种稀疏数组类型所使用的哈希算法是Cuckoo哈希算法:这可以在计算哈希值后只进行两次内存访问即可访问任何位置。
与HashMap类型的基准比较
Rust 1.70.0
Benchmarking store_hashmap_100k_to_self: Collecting 100 samples in estimated 5.6
store_hashmap_100k_to_self
time: [7.0570 ms 7.0644 ms 7.0729 ms]
Found 4 outliers among 100 measurements (4.00%)
1 (1.00%) high mild
3 (3.00%) high severe
Benchmarking store_sparse_array_100k_to_self: Collecting 100 samples in estimate
store_sparse_array_100k_to_self
time: [5.0491 ms 5.1826 ms 5.3174 ms]
Benchmarking fetch_hashmap_100k: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 8.5s, enable flat sampling, or reduce sample count to 50.
Benchmarking fetch_hashmap_100k: Collecting 100 samples in estimated 8.4986 s (5
fetch_hashmap_100k time: [1.6831 ms 1.6845 ms 1.6859 ms]
Found 6 outliers among 100 measurements (6.00%)
3 (3.00%) high mild
3 (3.00%) high severe
Benchmarking fetch_sparse_array_100k: Collecting 100 samples in estimated 5.5865
fetch_sparse_array_100k time: [269.83 µs 270.08 µs 270.38 µs]
Found 13 outliers among 100 measurements (13.00%)
1 (1.00%) low severe
7 (7.00%) low mild
3 (3.00%) high mild
2 (2.00%) high severe
Benchmarking insert_overwrite_hashmap_100k: Collecting 100 samples in estimated
insert_overwrite_hashmap_100k
time: [2.5645 ms 2.8273 ms 3.0931 ms]
Benchmarking insert_overwrite_sparse_array_100k: Collecting 100 samples in estim
insert_overwrite_sparse_array_100k
time: [536.66 µs 538.18 µs 539.75 µs]
Benchmarking update_hashmap_100k: Collecting 100 samples in estimated 5.1938 s (
update_hashmap_100k time: [2.9770 ms 3.2273 ms 3.4757 ms]
Benchmarking update_sparse_array_100k #2: Collecting 100 samples in es
insert_overwrite_sparse_array_100k #2
time: [258.76 µs 259.54 µs 260.43 µs]
Found 4 outliers among 100 measurements (4.00%)
2 (2.00%) high mild
2 (2.00%) high severe
依赖关系
~305KB