最近在爲推薦服務做性能調優,這個服務的主要邏輯是用離線計算的模型數據給請求中的每一個廣告打分,再返回這些廣告的排序結果,這裏面打分的過程其實就用請求中的數據拼成各類key,去查一個大的 map,這種計算很是多,成爲了主要的性能瓶頸,代碼比較老,使用的是 boost::unordered_map,爲了解決這個問題,找了一些第三方庫和標準庫對比了一下html
下面是在一臺 aws r4.xlarge
機器上的測試結果(注意編譯的時候必定要加 -O2):git
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 倍,這個性能和操做系統以及庫版本也有必定關係,實際生產環境中建議把代碼拉下來在本身的環境下測試一下github
咱們線上用 tsl::robin_map 替換了原來的 boost::unordered_map,總體性能提高了 5 倍,這裏面固然也還包含了一些其餘的優化,這個優化算是比較大的優化點了性能
轉載請註明出處
本文連接: http://www.hatlonely.com/2018...