c++ - 为什么 std::vector<uint8_t>::insert 使用 MSVC 2015 编译器时比 std::copy 快 5 倍?

标签 c++

我有一个简单的函数,可以将一个字节 block 复制到 std::vector:

std::vector<uint8_t> v;

void Write(const uint8_t * buffer, size_t count)
{
    //std::copy(buffer, buffer + count, std::back_inserter(v));

    v.insert(v.end(), buffer, buffer + count);
}

v.reserve(<buffer size>);
v.resize(0);

Write(<some buffer>, <buffer size>);

如果我使用 std::vector<uint8_t>::insert它比我使用 std::copy 快 5 倍.

我尝试使用启用和禁用优化的 MSVC 2015 编译这段代码,但得到了相同的结果。

看起来 std::copy 有点奇怪或 std::back_inserter实现。

最佳答案

标准库实现是在考虑性能的情况下编写的,但只有在优化开启时才能实现性能。

//This reduces the performance dramatically if the optimization is switched off.

尝试在优化关闭的情况下测量函数性能与 asking ourselves if the law of gravitation would still be true if there were no mass left in the Universe 一样毫无意义.

关于c++ - 为什么 std::vector<uint8_t>::insert 使用 MSVC 2015 编译器时比 std::copy 快 5 倍?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51930042/

相关文章:

C++ 代码库从 MFC 重写为 *nix

c++ - "enum class"在 Visual C++ 2012 中是什么意思?

c++ - 配置文件引导优化 (C)

c++ - Winsock ~ 创建一个 UDP 监听器(多个与 1 个套接字)

python - 计算两个日期之间有多少个星期一

c++ - 真的需要在 .h 和 .cpp 文件中分离 C++ 结构吗?

c# - 为 C# 包装 native DLL

C++ 设计 : Pool and pointers VS client-server

c++ - mktime() 返回的意外值

c++ - 使用 libcurl 和 Fiddler 拦截 HTTPS 流量