[英]Performance comparison: f(std::string&&) vs f(T&&)
[英]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 顯示以下結果:
鏗鏘聲12:
因此,如果您確實關心性能,請使用 strstr。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.