replace_copy的效率问题
{
//replace:前两个参数为目标序列,第三个参数为被替换值,第四参数为替换值
const int size = 1e7;
vector<int> vecInt;
vecInt.resize(size);
fill(vecInt.begin(), vecInt.end(), 10);
int time0 = GetTickCount();
replace(vecInt.begin(), vecInt.end(), 10, 20);
time0 = GetTickCount() - time0; //time0 = 16
time0 = GetTickCount();
for (int i = 0; i < size; ++i)
{
vecInt[i] == 10 ? vecInt[i] = 20 : 0;
}
time0 = GetTickCount() - time0; //time0 = 468
//replace_copy的速度令人发指,比手工操作还慢
vector<int> vecInt1;
time0 = GetTickCount();
replace_copy(vecInt.begin(), vecInt.end(), back_inserter(vecInt1), 20, 30);
time0 = GetTickCount() - time0; //time0 = 3058
vector<int> vecInt2;
time0 = GetTickCount();
for (int i = 0; i < size; ++i)
{
if (vecInt[i] == 20)
{
vecInt2.push_back(30);
}
else
{
vecInt2.push_back(vecInt[i]);
}
}
time0 = GetTickCount() - time0; //time0 = 2808
printf("\n");
}
如上代码,为什么replace_copy的效率还不如手写的效率高?
//replace:前两个参数为目标序列,第三个参数为被替换值,第四参数为替换值
const int size = 1e7;
vector<int> vecInt;
vecInt.resize(size);
fill(vecInt.begin(), vecInt.end(), 10);
int time0 = GetTickCount();
replace(vecInt.begin(), vecInt.end(), 10, 20);
time0 = GetTickCount() - time0; //time0 = 16
time0 = GetTickCount();
for (int i = 0; i < size; ++i)
{
vecInt[i] == 10 ? vecInt[i] = 20 : 0;
}
time0 = GetTickCount() - time0; //time0 = 468
//replace_copy的速度令人发指,比手工操作还慢
vector<int> vecInt1;
time0 = GetTickCount();
replace_copy(vecInt.begin(), vecInt.end(), back_inserter(vecInt1), 20, 30);
time0 = GetTickCount() - time0; //time0 = 3058
vector<int> vecInt2;
time0 = GetTickCount();
for (int i = 0; i < size; ++i)
{
if (vecInt[i] == 20)
{
vecInt2.push_back(30);
}
else
{
vecInt2.push_back(vecInt[i]);
}
}
time0 = GetTickCount() - time0; //time0 = 2808
printf("\n");
}
如上代码,为什么replace_copy的效率还不如手写的效率高?
...全文
相关推荐
STL_Cheat_Sheets:C ++ STL速查表
- **拷贝算法**:copy()、copy_if()将元素从一个容器复制到另一个容器。 - **替换算法**:replace()、replace_if()、swap()等修改元素值。
【053】C++ STL 算法精讲:copy、replace、swap 用好了能省一半代码!
本文介绍 C++ STL 中的 `copy`、`replace` 和 `swap` 算法,包括原型、参数、用法示例和注意事项。
52fuzhiwenjian.rar_Java编程_Java_
(source, target, StandardCopyOption.REPLACE_EXISTING); } } ``` 在这个例子中,`Files.copy()`方法直接实现了文件的复制,如果目标文件已存在
Java实现ftp文件上传下载解决慢中文乱码多个文件下载等问题
= null) { Files.copy(out, Paths.get(localFilename), StandardCopyOption.REPLACE_EXISTING); out.close()
python 在右键菜单中加入复制目标文件的有效存放路径(单斜杠或者双反斜杠)
. """ path_1 = self.path.replace('\\', '\\\\') return self.copy_to_clipboard(path_1) def get_path_2(self


