C++ 将 std::vector<int> 的每个值重置为 0 的最快方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8848575/
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
Fastest way to reset every value of std::vector<int> to 0
提问by Matthieu Riegler
What's the fastest way to reset every value of a std::vector<int>
to 0 and keeping the vectors initial size ?
将 a 的每个值重置std::vector<int>
为 0 并保持向量初始大小的最快方法是什么?
A for loop with the [] operator ?
带有 [] 运算符的 for 循环 ?
回答by Cat Plus Plus
std::fill(v.begin(), v.end(), 0);
回答by Fabio Fracassi
As always when you ask about fastest: Measure! Using the Methods above (on a Mac using Clang):
与往常一样,当您询问最快时:测量!使用上述方法(在 Mac 上使用 Clang):
Method | executable size | Time Taken (in sec) |
| -O0 | -O3 | -O0 | -O3 |
------------|---------|---------|-----------|----------|
1. memset | 17 kB | 8.6 kB | 0.125 | 0.124 |
2. fill | 19 kB | 8.6 kB | 13.4 | 0.124 |
3. manual | 19 kB | 8.6 kB | 14.5 | 0.124 |
4. assign | 24 kB | 9.0 kB | 1.9 | 0.591 |
using 100000 iterations on an vector of 10000 ints.
在 10000 个整数的向量上使用 100000 次迭代。
Edit:If changeing this numbers plausibly changes the resulting times you can have someconfidence (not as good as inspecting the final assembly code) that the artificial benchmark has not been optimized away entirely. Of course it is best to messure the performance under real conditions. end Edit
编辑:如果振振有词量变到质变这一数字变化所产生的时候,你可以有一些信心(还不如检查最终汇编代码),人工基准尚未完全优化掉。当然,最好在实际条件下测量性能。 结束 编辑
for reference the used code:
参考使用的代码:
#include <vector>
#define TEST_METHOD 1
const size_t TEST_ITERATIONS = 100000;
const size_t TEST_ARRAY_SIZE = 10000;
int main(int argc, char** argv) {
std::vector<int> v(TEST_ARRAY_SIZE, 0);
for(size_t i = 0; i < TEST_ITERATIONS; ++i) {
#if TEST_METHOD == 1
memset(&v[0], 0, v.size() * sizeof v[0]);
#elif TEST_METHOD == 2
std::fill(v.begin(), v.end(), 0);
#elif TEST_METHOD == 3
for (std::vector<int>::iterator it=v.begin(), end=v.end(); it!=end; ++it) {
*it = 0;
}
#elif TEST_METHOD == 4
v.assign(v.size(),0);
#endif
}
return EXIT_SUCCESS;
}
Conclusion:use std::fill
(because, as others have said its most idiomatic)!
结论:使用std::fill
(因为,正如其他人所说的那样最惯用)!
回答by fredoverflow
How about the assign
member function?
如何在assign
成员函数?
some_vector.assign(some_vector.size(), 0);
回答by unwind
If it's just a vector of integers, I'd first try:
如果它只是一个整数向量,我会先尝试:
memset(&my_vector[0], 0, my_vector.size() * sizeof my_vector[0]);
It's not very C++, so I'm sure someone will provide the proper way of doing this. :)
它不是很 C++,所以我相信有人会提供正确的方法来做到这一点。:)
回答by nttstar
try
尝试
std::fill
and also
并且
std::size siz = vec.size();
//no memory allocating
vec.resize(0);
vec.resize(siz, 0);
回答by Yauhen Yakimenka
I had the same question but about rather short vector<bool>
(afaik the standard allows to implement it internally differently than just a continuous array of boolean elements). Hence I repeated the slightly modified tests by Fabio Fracassi. The results are as follows (times, in seconds):
我有同样的问题,但问题很短vector<bool>
(afaik 标准允许在内部实现它,而不仅仅是一个连续的布尔元素数组)。因此,我重复了 Fabio Fracassi 稍作修改的测试。结果如下(次,秒):
-O0 -O3
-------- --------
memset 0.666 1.045
fill 19.357 1.066
iterator 67.368 1.043
assign 17.975 0.530
for i 22.610 1.004
So apparently for these sizes, vector<bool>::assign()
is faster. The code used for tests:
所以显然对于这些尺寸,vector<bool>::assign()
速度更快。用于测试的代码:
#include <vector>
#include <cstring>
#include <cstdlib>
#define TEST_METHOD 5
const size_t TEST_ITERATIONS = 34359738;
const size_t TEST_ARRAY_SIZE = 200;
using namespace std;
int main(int argc, char** argv) {
std::vector<int> v(TEST_ARRAY_SIZE, 0);
for(size_t i = 0; i < TEST_ITERATIONS; ++i) {
#if TEST_METHOD == 1
memset(&v[0], false, v.size() * sizeof v[0]);
#elif TEST_METHOD == 2
std::fill(v.begin(), v.end(), false);
#elif TEST_METHOD == 3
for (std::vector<int>::iterator it=v.begin(), end=v.end(); it!=end; ++it) {
*it = 0;
}
#elif TEST_METHOD == 4
v.assign(v.size(),false);
#elif TEST_METHOD == 5
for (size_t i = 0; i < TEST_ARRAY_SIZE; i++) {
v[i] = false;
}
#endif
}
return EXIT_SUCCESS;
}
I used GCC 7.2.0 compiler on Ubuntu 17.10. The command line for compiling:
我在 Ubuntu 17.10 上使用了 GCC 7.2.0 编译器。用于编译的命令行:
g++ -std=c++11 -O0 main.cpp
g++ -std=c++11 -O3 main.cpp