C++ 使用 memcpy 将 std::vector 从 protobuf 复制到重复字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15499641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copy a std::vector to a repeated field from protobuf with memcpy
提问by akristmann
At first I have this simple protobuf file
起初我有这个简单的 protobuf 文件
message messagetest
{
...
repeated float samples = 6;
....
}
Which creates a headerfile with this methods
使用此方法创建头文件
//repeated float samples = 6;
inline int samples_size() const;
inline void clear_samples();
static const int kSamplesFieldNumber = 6;
inline float samples(int index) const;
inline void set_samples(int index, float value);
inline void add_samples(float value);
inline const ::google::protobuf::RepeatedField< float >& samples() const;
inline ::google::protobuf::RepeatedField< float >* mutable_samples();
What I'm basically doing is to copy all data one by one in a for loop.
我基本上做的是在 for 循环中一一复制所有数据。
int main(int argc, char** argv)
{
messagetest fMessage;
vector<float> fData (1000, 0);
// Create 1000 random values
for (int i = 0; i < fData.size(); i++)
{
fData[i] = rand() % 1001;
}
for (int j = 0; j < fData.size(); j++)
{
fMessage.add_samples(fData[j]);
}
return 0;
}
But I want to use a method like memcpy to accelerate the copy process. It is just an idea that comes to my mind. If it's completely wrong correct me. The last declaration in the headerfile is:
但是我想使用像 memcpy 这样的方法来加速复制过程。这只是我想到的一个想法。如果完全错误,请纠正我。头文件中的最后一个声明是:
inline ::google::protobuf::RepeatedField< float >* mutable_samples();
I have no idea what this method does (lack of skill). But it kind of looks like a vector. Maybe that's the solution for my problem. If so, I have no idea how to implement it.
我不知道这种方法有什么作用(缺乏技巧)。但它有点像一个向量。也许这就是我的问题的解决方案。如果是这样,我不知道如何实现它。
Edit SOLVED:
编辑已解决:
Memcpy is in any case faster, because you have one method call and then you copy the data. Compared to the for loop which has to call the "fMessage.add_samples()" method 1000 times.
Memcpy 在任何情况下都更快,因为您有一个方法调用,然后您复制数据。与必须调用“fMessage.add_samples()”方法 1000 次的 for 循环相比。
One flaw of this approach is that you have to know the size of your vector to reserve data for your samples, but then you can allocate at programm start the memory for fMessage.sample (this works for me).
这种方法的一个缺陷是您必须知道向量的大小才能为您的样本保留数据,但是您可以在程序启动时为 fMessage.sample 分配内存(这对我有用)。
fMessage.mutable_samples()->Reserve(fData.size());
for (int j = 0; j < fData.size(); j++)
{
fMessage.add_samples(0);
}
And now i can get the data from a stream in a while loop and copy them with memcpy to my datastructer
现在我可以在 while 循环中从流中获取数据并使用 memcpy 将它们复制到我的数据结构中
while(42)
{
//fancy streaming things to get vector fData
memcpy(fMessage.mutable_samples()->mutable_data(),
&fData[0],
sizeof(float)*fData.size());
}
回答by mgild
Since this isn't here yet and I like one-liners:
由于这还不在这里,我喜欢单线:
*fMessage.mutable_samples() = {fData.begin(), fData.end()};
回答by nazgul
I found the shortest way to copy vector into repeated field as this:
我找到了将向量复制到重复字段的最短方法,如下所示:
google::protobuf::RepeatedField<float> data(fData.begin(), fData.end());
fMessage.mutable_samples()->Swap(&data);
it is probably also the faster than yours, since it avoids initial iteration and setting values to 0
它可能也比你的更快,因为它避免了初始迭代并将值设置为 0
回答by Arthur George
fMessage.mutable_samples()
return an array of pointer of samples : [*sample1, *sample2, sample3, ...].
返回一个样本指针数组:[*sample1, *sample2, sample3, ...]。
&fData[0]
is the address of first element of fData.
是 fData 的第一个元素的地址。
memcpy(fMessage.mutable_samples()->mutable_data(),
&fData[0],
sizeof(float)*fData.size());
So I do not think the code above can successfully fill data from fData to fMessage. It's totally wrong!
所以我不认为上面的代码可以成功地将数据从fData填充到fMessage。完全错了!