簡體   English   中英

性能比較:strstr() 與 std::string::find()

[英]Performance comparison: strstr() vs std::string::find()

有人可以解釋為什么我應該使用 strstr 或 string find() 嗎? 哪個更快,在哪里?

在C ++中你應該使用std :: string :: find(),在C中你應該使用strstr()。 性能差異不應太大。

哪個更快更沒關系。 更重要的是std :: string :: find是安全的。 因此,如果要使用c ++,請使用std :: string類並避免使用舊的c函數。

我已經對字符串中搜索子字符串的幾個版本進行了基准測試。

帶有 std=c++20/-O3 的 GCC 10.3 顯示以下結果:

  • strstr 是最快的
  • std::string::find 慢約 3 倍
  • std::search 慢約 4.5 倍
  • std::string_view::find ~ 慢 11 倍

鏗鏘聲12:

  • strstr 是最快的
  • std::string::find 慢約 3 倍
  • std::search 慢約 5.5 倍
  • std::string_view::find ~ 慢 8.5 倍

因此,如果您確實關心性能,請使用 strstr。

暫無
暫無

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

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