c – 无序映射中重新计算的计数
发布时间:2020-12-16 06:53:12 所属栏目:百科 来源:网络整理
导读:评估unordered_map性能的正确方法是什么? [C 14] 在我的代码中,我使用std :: unordered_map非常广泛地按照数十亿个键的顺序.出于性能的目的,我希望知道unordered_map的行为,它必须重新散列多少次以及所有其他参数(多少个桶?在重新散列之前有多少桶空?).我
评估unordered_map性能的正确方法是什么? [C 14]
在我的代码中,我使用std :: unordered_map非常广泛地按照数十亿个键的顺序.出于性能的目的,我希望知道unordered_map的行为,它必须重新散列多少次以及所有其他参数(多少个桶?在重新散列之前有多少桶空?).我知道stl提供了桶的数量.但是还需要配置什么或者您用什么来分析? 解决方法
与许多std容器一样,unordered_map被强制指数级增长.确切的速率是实现定义;您可以检查实现的规范或其源代码.
如何调整大小是确定性的.如果将其包装在垫片中,则可以通过在允许生长容器的每个操作之前和之后检查bucket_count来检测这些调整大小. 您可以遍历存储桶: template<class UnorderedMeow> std::size_t empty_buckets( UnorderedMeow&& meow ) { std::size_t r = 0; auto buckets = meow.buckets_count(); for (decltype(buckets) i = 0; i < buckets; ++i) if (meow.bucket_size(i)==0) ++r; return r; } 找出有多少是空的. 如果您使用基于继承的合成并仅覆盖您知道可以添加/删除内容的那些… template<class Base> struct instrumented_unordered_map:Base { using Self = instrumented_unordered_map; using Base::Base; using key_type = Base::key_type; // etc struct instrument { Self* self; instrument( Self* s ):self(s) { self->start_instrument(); } ~instrument() { self->end_instrument(); } }; struct instrument_state { // blah }; instrument_state state; void start_instrument() { // populate state } void end_instrument() { // extract from state,generate report } decltype(auto) operator[]( key_type const& key ) { instrument _(this); return Base::operator[](key); } // etc }; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |