问题描述
在我的C ++应用程序中,我大量使用STL容器,如 vector
。有很多调用 push_back
,我一直关注不必要的构造和复制操作。
In my C++ application I heavily use STL containers like vector
. There are a lot of calls to push_back
, and I have been concerned about unnecessary constructions and copy operations.
我的应用程序是相当低级别,我非常关心CPU和内存使用。是否应该调用 emplace_back
来取代 push_back
的所有调用?
My application is pretty low-level and I am very concerned about CPU and memory usage. Should I replace all calls to push_back
with calls to emplace_back
?
我使用Visual Studio 2013。
I am using Visual Studio 2013.
推荐答案
我取代了所有调用 push_back
调用 emplace_back
并注意到以下内容:
I replaced all calls to push_back
with calls to emplace_back
and noticed the following:
- RAM使用(更新:这可能是由于其他效果)
- CPU使用率未变。
- 二进制文件稍小(x64)
- 没有兼容性问题。
- RAM usage is reduced by approximately 20% (update: this may have been due to other effects)
- CPU usage is unchanged
- The binary is slightly smaller (x64)
- There were no compatibility problems
这些经验我可以强烈推荐从 push_back
到 emplace_back
的移动,如果你的项目不需要向后 - 与旧编译器兼容。
Based on these experiences I can highly recommend to make the move from push_back
to emplace_back
if your project does not need to be backwards-compatible with older compilers.
这篇关于我应该用emplace_back替换所有调用push_back?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!