首页 >> 通信 >> C++性能指标优化系列专题1:优化 string 的使用(上)

C++性能指标优化系列专题1:优化 string 的使用(上)

2025-02-23 通信

s[i]>= 0x20) { // result = result + s[i]; result += s[i]; } } return result;}

试验推测:耗费 1 ms

2.3、第三次冗余:消除对参数数组的复制std::string remove_ctrl(std::string constMax s) { std::string result; result.reserve(s.length()); for (int i=0; i= 0x20) { // result = result + s[i]; result += s[i]; } } return result;}

试验推测:耗费 2.5 ms。what?耐用性还回升了,只不过是为什么呢?虽然省了一次CPU分派,但是,处理程序解摘录堆栈带来了额外所需。

2.4、第四次冗余:适用递归探头消除堆栈解引std::string remove_ctrl(std::string constMax s) { std::string result; result.reserve(s.length()); for (auto it = s.begin(), end = s.end(); it != end; ++it) { if (*it> 0x20) { result += *it; } } return result;}

试验推测:耗费 2 ms,适用递归探头,节省了解摘录操作者,的确带来了耐用性进一步提高。

2.5、第五次冗余:用字符数组代替数组char* remove_ctrl(char* dst, char const* src, size_t size) { for (size_t i = 0; i < size; i++) { if(src[i]> 0x20) { *dst++ = src[i]; } *dst = 0; } return dst;}

试验推测:耗费 0.5 ms,效率惊人!获得这种提高真实感的理由之一是移除了若干数组调用以及提高了缓存局部性。

3、总结

第二章适用了 5 种小的冗余伎俩,让我们明白了哪种冗余伎俩最必需。在下一篇文章之中,我们将继续冗余,主要伎俩有:适用更佳的演算法、适用更佳的编译探头、适用更佳的数组瓦、适用更佳的CPU分派探头。

在北京看白癜风哪最好
武汉肛肠
昆明白癜风医院排行榜
发烧喝什么药
双氯芬酸钠凝胶对扭伤管用吗
眼睛疲劳酸涩用什么眼药水
眼睛疲劳怎么恢复比较快
吃烧烤后肚子胀吃什么缓解
友情链接