簡體   English   中英

unordered_set 與向量——更喜歡慣用的還是高性能的?

[英]unordered_set vs vector -- prefer idiomatic or performant?

我正在處理與其他同類數據不同的數據。 非常抽象地說,一set符合我正在使用的數據的定義。 出於這個原因,我傾向於使用std::unordered_set而不是std::vector

除此之外,這兩個課程都可以滿足我的要求。 我的問題是關於性能的——哪個性能更好? 我不能以一種方式寫出代碼並對其進行基准測試,然后以另一種方式重寫它。 這將花費我數百小時。 如果它們的表現相似,您認為堅持使用慣用的unordered_set是否值得?

這是一個更簡單的用例。 一家公司正在銷售電腦。 每一個都至少在一個方面是獨一無二的,保證。

struct computer_t
{
    std::string serial;
    std::uint32_t gb_of_ram;
};
std::unordered_set<computer_t> all_computers_in_existence;
std::unordered_set<computer_t> computers_for_sale; // subset of above
// alternatively
std::vector<computer_t> all_computers_in_existence;
std::vector<computer_t> computers_for_sale; // subset of above

該公司希望停止銷售不受歡迎的計算機,並用其他可能受歡迎的計算機來代替它們。

std::unordered_set<computer_t> computers_not_for_sale;
std::set_difference(all_computers_in_existence.begin(), all_computers_in_existence.end(),
                    computers_for_sale.begin(), computers_for_sale.end(),
                    std::inserter(computers_not_for_sale, computers_not_for_sale.end()));

calculate_and_remove_least_sold(computers_for_sale);
calculate_and_add_most_likely_to_sell(computers_for_sale, computers_not_for_sale);

基於上面的示例代碼,我應該選擇什么? 還是我應該調查另一個新的 STL 功能(在 C++17 中)? 對於我的用例來說,這確實是通用的,而不會使這篇文章的細節變得非常長。

慣用語應該是您的首選。 如果您使用 unordered_set 實現它並且性能不夠好,則有更快的非 STL hash 表可以輕松切換到。 99% 的時間都不會這樣。

您使用std::set_difference的示例代碼將不起作用,因為這需要對輸入進行排序,而unordered_set不是。 沒關系,使用unordered_set::erase(key)可以輕松完成減法。

幾百小時?

您創建一個新的 class “計算機列表”,其中一個無序集或 std::vector 作為唯一成員。 你用這個結構替換所有的 std::vector<computer_t> 。 由於調用向量 function 而無法編譯的任何內容,將內聯 function 添加到此 class 執行相同的操作。 最壞的情況應該花費你幾個小時。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM