使用memcpy将std :: vector复制到protobuf的重复字段中 [英] Copy a std::vector to a repeated field from protobuf with memcpy

查看:1201
本文介绍了使用memcpy将std :: vector复制到protobuf的重复字段中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

起初我有一个简单的protobuf文件

At first I have this simple protobuf file

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();

我基本上要做的是在for循环中一张一张地复制所有数据.

What I'm basically doing is to copy all data one by one in a for loop.

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;
}

但是我想使用类似memcpy的方法来加速复制过程.这只是一个想法.如果完全错误,请纠正我. 头文件中的最后一个声明是:

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:

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.

编辑已解决:

在任何情况下,Memcpy都更快,因为您有一个方法调用,然后复制数据.与for循环相比,该循环必须调用"fMessage.add_samples()"方法1000次.

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.

这种方法的一个缺点是,您必须知道向量的大小才能为样本保留数据,但是随后您可以在编程时分配fMessage.sample的内存(这对我有用).

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.mutable_samples()->Reserve(fData.size());

for (int j = 0; j < fData.size(); j++)
{
   fMessage.add_samples(0);          
}

现在我可以在while循环中从流中获取数据,并使用memcpy将其复制到我的datastructer中.

And now i can get the data from a stream in a while loop and copy them with memcpy to my datastructer

while(42)
{
   //fancy streaming things to get vector fData

  memcpy(fMessage.mutable_samples()->mutable_data(),
         &fData[0],
         sizeof(float)*fData.size());
}

推荐答案

由于此功能尚不存在,所以我喜欢单线:

Since this isn't here yet and I like one-liners:

*fMessage.mutable_samples() = {fData.begin(), fData.end()};

这篇关于使用memcpy将std :: vector复制到protobuf的重复字段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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