首頁  >  文章  >  後端開發  >  案例分享c++ map的使用和 查找效能測試

案例分享c++ map的使用和 查找效能測試

php是最好的语言
php是最好的语言原創
2018-08-02 10:36:172253瀏覽

最近在為推薦服務作效能調優,這個服務的主要邏輯是用離線計算的模型資料給請求中的每個廣告打分,再返回這些廣告的排序結果,這裡面打分的過程其實就用請求中的資料拼成各種key,去查一個大的map,這種計算非常多,成為了主要的效能瓶頸,程式碼比較老,使用的是boost::unordered_map,為了解決這個問題,找了一些第三方函式庫和標準函式庫對比了一下

下面是在一台aws r4.xlarge 機器上的測試結果(注意編譯的時候一定要加-O2):

std::map<int, int>                                 => 51866903
std::unordered_map<int, int>                       => 3838175
std::unordered_map<int, int, nohashint>            => 3508570
std::unordered_map<int, int>(N)                    => 3804471
boost::unordered_map<int, int>                     => 3291384
boost::unordered_map<int, int, nohashint>          => 3293934
boost::unordered_map<int, int>(N)                  => 3265856
google::dense_hash_map<int, int>                   => 785969
google::dense_hash_map<int, int, nohashint>        => 784455
google::dense_hash_map<int, int>(N)                => 899262
tsl::hopscotch_map<int, int>                       => 654668
tsl::hopscotch_map<int, int, nohashint>            => 680964
tsl::hopscotch_map<int, int>(N)                    => 663607
tsl::robin_map<int, int>                           => 406176
tsl::robin_map<int, int, nohashint>                => 411358
tsl::robin_map<int, int>(N)                        => 409993

可以看到tsl::robin_map 的效能基本上能達到std::unordered_map 的10 倍,這個效能和作業系統以及函式庫版本也有一定關係,實際生產環境中建議把程式碼拉下來在自己的環境下測試一下

我們線上用tsl::robin_map 取代了原來的boost::unordered_map,整體效能提升了5 倍,這裡面當然也還包含了一些其他的優化,這個優化算是比較大的最佳化點了

相關文章:

##MySQL慢查詢查找和調優測試

php函數的尋找效能測試

相關影片:

C 手冊教學

以上是案例分享c++ map的使用和 查找效能測試的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn