std::vector::emplace_back 比 std::vector::push_back 慢的例子? [英] Examples where std::vector::emplace_back is slower than std::vector::push_back?

查看:69
本文介绍了std::vector::emplace_back 比 std::vector::push_back 慢的例子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在阅读 Scott Meyer 的Effective Modern C++".在第 42 项中,他声称例如std::vector::emplace_back 通常(但不总是)与使用 push_back 一样快甚至更快.他列出了至少应该达到同样快的三个条件,但在这些条件不全部满足的情况下,他没有提供反例.有人可以为我提供一个示例,其中使用 emplace_back 预计会导致比使用 push_back 更差的性能吗?

I am currently reading Scott Meyer's "Effective Modern C++". In Item 42, he claims that e.g. an std::vector::emplace_back is usually, but not always, as fast as or even faster than using push_back. He lists three conditions under which it should be at least as fast, but does not provide a counterexample in the case where these conditions are not all satisfied. Can someone provide me with an example where using emplace_back would be expected to result in strictly worse performance than using push_back?

推荐答案

本质上这归结为 std 实现.从理论上讲,emplace 应该总是一样快或更快,但现实情况是没有标准库实现充分利用了这一点.

Essentially this boils down to std implementations. Theoretically, emplace should always be as fast or faster, except the reality is that no standard library implementation takes full advantage of that.

几年前他就这个确切的问题进行了一次演讲:https://www.youtube.com/watch?t=3427&v=smqT9Io_bKo

He gave a talk on this exact issue a few years ago: https://www.youtube.com/watch?t=3427&v=smqT9Io_bKo

查看演讲的前 1 小时以获得更详细的解释.演讲结束时的问答也很重要.

Check out the first 1 hour of the talk for a more detailed explanation. The Q&A at the end of the talk is relevant as well.

这篇关于std::vector::emplace_back 比 std::vector::push_back 慢的例子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆