- 论坛徽章:
- 9
|
这两天在测试 自己重写的std::unordered_hash库, 主要的变化有几点:
1)去掉了标准算法容器等效于 vector<list<T>>, 改进后的算法 容器等效于boost::circular_buffer<T>, 由于有更好的cache依存性, 在多数情况下性能会更好
2)算法中容器默认为2的倍数, 每次都按照2的倍数扩展或者减少, 这样去掉了消耗资源多的除法运算, 用&代替, hash值的匹配更快速
3)rehash的改进: 将rehash中copy T过程, 对于旧容器进行依次pop, 及时释放内存, Copy一个释放一个, 而不是一次性释放, 减少了rehash过程中的内存占用.
4)删除中添加检测并 自动rehash, 及时减少内存占用.
5)hash值的缓存, 将每个Key的hash缓存下来,多占用4个字节, 但是rehash的时候就不用重新计算了, 提高了insert性能.
总体上内存占用率下降20%以上, 综合性能提高70%以上.
下为测试情况:
guo@guo-desktop:/mnt/guo/newcpp/hash_map/bin/Release$ ./hash_map
TEST_COUNT=10000000, std::unordered_map<std::string, std::shared_ptr<std::string> > map1; // 依次为insert , find , delete的花费时间,
6.076009s wall, 5.880000s user + 0.180000s system = 6.060000s CPU (99.7%)
3.253689s wall, 3.250000s user + 0.000000s system = 3.250000s CPU (99.9%)
4.088779s wall, 4.080000s user + 0.000000s system = 4.080000s CPU (99.8%)
Log: Time=2016-11-06T19:17:10.074099, Line=75, File="/mnt/guo/newcpp/hash_map/main.cpp", Func="test_myhash", Msg="Circular_hash_map map2"
3.652572s wall, 3.610000s user + 0.050000s system = 3.660000s CPU (100.2%)
2.086055s wall, 2.080000s user + 0.000000s system = 2.080000s CPU (99.7%)
3.389029s wall, 3.360000s user + 0.030000s system = 3.390000s CPU (100.0%) // 主要是delete中 多次rehash回收了内存, 如果去掉rehash,速度会更快
|
|