简体   繁体   English

将原始字节数组复制到空字节向量的最有效方法

[英]Most efficient way of copying a raw byte array into an empty byte vector

I have a scenario in which I need to copy the contents of a raw dynamically allocated uint8_t array into a vector (which is guaranteed to be empty whenever this scenario happens). 我有一个场景,我需要将原始动态分配的uint8_t数组的内容复制到一个向量中(无论何时发生这种情况,都保证为 )。

vector<uint8_t> myVector;
const uint8_t* myRawArray;

It is really important to me that the copy operation is as efficient as possible and portable (various compiler versions might be used). 对我来说非常重要的是复制操作尽可能高效且可移植(可能会使用各种编译器版本)。

One approach I thought of using is this: 我想用的一种方法是这样的:

myVector.reserve(byteCount);
myVector.insert(myVector.begin(), myRawArray, myRawArray + byteCount);

Any ideas on how the speed of that compares to this one: 关于速度如何与此相比的任何想法:

myVector.resize(byteCount);
memcpy(myVector.data(), myRawArray, byteCount);

I guess memcpy should be fast but then I am forced to use resize which needs to zero-out the memory, so I guess it will slow it down a bit.. 我想memcpy应该很快,但后来我被迫使用resize ,需要将内存清零,所以我想它会慢下来一点..

Also, any other suggestions? 还有,还有其他建议吗?

If you don't need to create the vector before the copy takes place, you could always pass the raw array to the constructor of your vector: 如果在复制发生之前不需要创建向量,则可以始终将原始数组传递给向量的构造函数:

std::vector<uint8_t> myVector(myRawArray, myRawArray + byteCount);

If you do need to construct the vector beforehand, the following is an option: 如果您确实需要事先构造向量,则以下是一个选项:

std::vector<uint8_t> myVector;
// ... do some stuff ...
// Now, we're ready for the copy, and byteCount is known.
myVector.reserve(byteCount);
std::copy(myRawArray, myRawArray + byteCount, std::back_inserter(myVector));

I would suggest using std::copy unless memcpy is proven to be faster. 我建议使用std :: copy,除非memcpy被证明更快。 std::copy is safer and more idiomatic in C++ code, but don't be afraid to use memcpy if it really is proven to be faster. std :: copy在C ++代码中更安全,更惯用,但如果真的被证明更快,不要害怕使用memcpy。 The speed difference will most likely change with different compilers. 速度差异很可能会随着不同的编译器而改变。

I hope this helps. 我希望这有帮助。

memcpy() is usually written in assembly and it is very optimized so you should know that memcpy will be fast. memcpy()通常用汇编语言编写而且非常优化,所以你应该知道memcpy会很快。 vector::insert is usually implemented as having a call to memcpy under the hood but it does need to check if there is enough space in the vector for the insertions to take place without any reallocations. vector::insert通常实现为在引擎盖下调用memcpy ,但它确实需要检查向量中是否有足够的空间来进行插入而不进行任何重新分配。 I have not profiled this but I bet the first version with the call to reserve is faster. 我没有对此进行过分析,但我打赌第一个版本的reserve呼叫更快。

An alternative to this would be to use std::copy which has been found to be slightly faster than using memcpy in some cases, you can be sure that if possible it also makes a call to memcpy or does something better. 另一种方法是使用std::copy ,在某些情况下发现它比使用memcpy稍快一些,你可以肯定,如果可能的话,它也会调用memcpy或做更好的事情。 So performance issues should not be a problem with it. 所以性能问题应该不是问题。 It will also take care of increasing the size of the vector to match your requirement. 它还将注意增加矢量的大小以满足您的要求。

Thanks all for your input to my issue I have resolve the problem by doing the following changes to my structure and implementing it like this 感谢大家对我的问题的投入,我通过对我的结构进行以下更改并像这样实现它来解决问题

   struct YUV_Buffer
   {
void *pCacheBuf = nullptr;
int frameID = 0;
int height = 0;
int width = 0;
void CopyBuf(BYTE * pBuf, int sizBuf)
{
    pCacheBuf = new BYTE[sizBuf];
    memcpy(pCacheBuf, pBuf, sizBuf);
}

YUV_Buffer(BYTE * pBuf, int nFrameID, int nHeight, int nWidth)
    : frameID(nFrameID), height(nHeight), width(nWidth)
{
    CopyBuf(pBuf, 8 * 1024 * 1024);
}

YUV_Buffer(const YUV_Buffer & yuvbuf)
    :frameID(yuvbuf.frameID), height(yuvbuf.height), width(yuvbuf.width)
{
    CopyBuf((BYTE*)yuvbuf.pCacheBuf, 8 * 1024 * 1024);
}
~YUV_Buffer() {
    delete[]pCacheBuf;
    pCacheBuf = NULL;
}
 };

I then implement it like this: 然后我像这样实现它:

  YUV_Buffer nBuffer = YUV_Buffer((BYTE*)pSysFrame, pmfxInSurface->Data.FrameOrder, pmfxInSurface->Info.CropH, pmfxInSurface->Info.CropW);
mBuffer.emplace_back(nBuffer);

Hope this may help others also complements to sarabande from expert-exchange for there help and input. 希望这可以帮助其他人也从专家交流中补充sarabande以获得帮助和投入。

Regards Nigel 关心奈杰尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 写入字节流的最有效方法 - Most efficient way of writing byte array for streaming 将std :: deque内容复制到byte-array的最有效方法 - Most efficient method of copying std::deque contents to byte-array 使用字节序列初始化数组的有效方法? - Efficient way to initialize array with a byte sequence? 将 C 字符串转换为 std::vector<byte> 以有效的方式 - Converting a C-string to a std::vector<byte> in an efficient way 使用联合测试字节数组中位的有效方法? - Efficient way to test bits in a byte array using a union? 将无符号char *从C ++作为字节[]传递给Java的最有效方法 - Most efficient way to pass unsigned char* from C++ to java as byte[] 将整数向量转换为字节数组的向量,然后调用每个字节数组 - Converting an integer vector into a vector of byte arrays then calling each byte array 复制在std向量中只出现一次的元素的最有效方法是什么? - What is the most efficient way of copying elements that occur only once in a std vector? 找到一种在 2 个巨大缓冲区上执行 MAX(每字节字节)的有效方法 - Find an efficient way to perform a MAX, byte per byte, on 2 huge buffers 将16位整数复制到两个字节的数组 - Copying a 16 bit integer to a two byte array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM