将数组填充到最大值(memcpy vs vector)
Fill an array to a maximum amount (memcpy vs vector)
我的类动态分配一个整数数组(在下面的代码中命名为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::queue
或std::deque
而不是std::vector
。
(在我写完这篇文章之后,我看到borisbn和C.R.已经在我之前几个小时提供了使用vector::erase的信息。
相关文章:
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 数据成员SFINAE的C++17测试:gcc vs clang
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 正在VS调试器中监视映射条目
- Confusion: decltype vs std::function
- 将IBM Rhapsody模型集成到VS 2019中
- VS Code "command":"make"与终端窗口中的命令行"make"不同
- 使用VS Code和CMake Tools运行自定义命令
- 修改 VS Code 中的默认C++代码段
- 如何使用c++在VS 2019上运行SQL查询
- std::memcpy vs std::copy_n for legacy c structs
- 由于Memcpy的问题,无法使用VS进行优化;被视为固有功能
- 库达型双关语 - memcpy vs UB 联盟
- 复制成数组时的memcpy vs分配;为什么这会生成不同的代码
- Memcpy vs Memmove - 调试与发布
- 德尔福复制记忆 vs C++ memcpy
- 将内置数据类型转换为std::string: memcpy vs snprintf
- 将数组填充到最大值(memcpy vs vector)