将数组填充到最大值(memcpy vs vector)

Fill an array to a maximum amount (memcpy vs vector)

本文关键字:memcpy vs vector 最大值 数组 填充      更新时间:2023-10-16

我的类动态分配一个整数数组(在下面的代码中命名为this_data),但是该数组只能填充到最大长度(this_data_maxlen)。我想添加数据(add_data)到数组,但是添加的数组可能超过最大长度。

知道new关键字没有重新分配,我写了一个版本使用memcpy,另一个版本使用vector,因为这被认为是最有效的方式。然而,在测试时,vector版本比memcpy慢5倍。我在vector版本做错了什么吗?

Memcpy版本:

unsigned int this_data_len=50;
unsigned int this_data_maxlen=70;           //maximum length of existing dataset
unsigned int add_data_len=50;
int *this_data = new int[this_data_len];    //existing dataset
int *add_data = new int[add_data_len];      //data we want to add to existing dataset
//function here that puts values in them
unsigned int new_data_len=min(this_data_maxlen,this_data_len+add_data_len);     //calculate the new dataset length (=7)
int *new_data=new int[new_data_len];        //create the new dataset
//build the new 'this_data'
memcpy(new_data,this_data,this_data_len*sizeof(int));   //copy existing dataset values to new dataset
memcpy(new_data+this_data_len,add_data,(this_data_maxlen-this_data_len)*sizeof(int));       //fill up the new dataset with a selection of the data to add
delete [] this_data;                        //remove original dataset
this_data=new_data;                         //set the new dataset
//build the new 'add_data'
add_data_len=add_data_len-(this_data_maxlen-this_data_len); //update the add_data length (=2)
new_data=new int[add_data_len];
memcpy(new_data,add_data+(this_data_maxlen-this_data_len),add_data_len*sizeof(int));
delete [] add_data;
add_data=new_data;
this_data_len=new_data_len;                 //set the new dataset length
//clean up
delete [] this_data;
delete [] add_data;
向量版本:

unsigned int this_data_len=50;
unsigned int this_data_maxlen=70;           //maximum length of existing dataset
unsigned int add_data_len=50;
vector<int> this_vector(this_data_len);
vector<int> add_vector(add_data_len);
//function here that puts values in them
unsigned int new_data_len=min(this_data_maxlen,this_data_len+add_data_len);     //calculate the new dataset length (=7)
this_vector.reserve(new_data_len);
this_vector.insert(this_vector.end(),add_vector.begin(),add_vector.begin()+(this_data_maxlen-this_data_len));
add_vector=vector<int>(add_vector.begin()+(this_data_maxlen-this_data_len),add_vector.end());
add_data_len=add_data_len-(this_data_maxlen-this_data_len); //update the add_data length (=2)
this_data_len=new_data_len;                 //set the new dataset length

您的测试似乎不相等。特别是,这一行可能比您预期的要做更多的工作:

add_vector = vector<int>(add_vector.begin() + (this_data_maxlen - this_data_len),
                         add_vector.end());

至少,这一行很可能创建了一个临时向量,删除了原向量的内容,并可能在删除前将临时向量的内容复制到原向量中。(使用RVO或c++ 11的move语义,其中一些可以避免。)

这些都是为了实现

的效果
add_vector.erase(add_vector.begin(),
                 add_vector.begin() + (this_data_maxlen - this_data_len));

作为另一个评论,如果你总是在末尾添加数据并从开始删除数据,你可能最好使用std::queuestd::deque而不是std::vector

(在我写完这篇文章之后,我看到borisbn和C.R.已经在我之前几个小时提供了使用vector::erase的信息。