将矢量归零的最快方法? [英] Fastest way to zero a vector?
问题描述
好的。
我有一个向量< bool>固定大小N.让我们把它叫做bob。
我很笨笨,即:
bob = vector< bool> (N,假);
当然,由于所有对象的创建,这很慢。
我可以做的比以下内容:
bob.clear(); bob.resize(N,false);
?
我不介意更改容器类型或条目类型(例如
bool-> char),所以如果你有任何建议,那就开火了
。
我正试图得到这个真的很快,所以我会招待任何所谓的极端解决方案。
Joseph
Okay.
I have a vector<bool> of fixed size N. Let''s call it bob.
I''m zero''ing bob quite a bit, i.e.:
bob = vector<bool>(N, false);
Naturally, this is quite slow, because of all the object creation.
Can I do any better than the following:
bob.clear(); bob.resize(N, false);
?
I don''t mind changing the container type or the entry type (e.g.
bool->char), so if you have any suggestions in that vein then fire
away.
I''m trying to get this to be really fast, so I''ll entertain any
so-called "extreme solution".
Joseph
推荐答案
哦。
我应该指出的另一件事是N非常小:
N = 3
(根据命令行的不同,它可能小于3,但不会超过
。)
Joseph
Oh.
The other thing I should point out is that N is really small:
N=3
(Depending upon the command-line, it may be less than 3, but not
greater.)
Joseph
Joseph Turian写道:
Joseph Turian wrote:
好的。
我有矢量< bool>固定大小N.让我们把它叫做bob。
我对于bob很零,即:
bob = vector< bool>(N,false);
当然,由于所有对象的创建,这都很慢。
我可以做得比以下更好:
bob.clear(); bob.resize(N,false);
?
我不介意更改容器类型或条目类型(例如
bool-> char),所以如果你有这方面的任何建议,那就开火了。
我正在努力让这个很快,所以我会招待任何
所以 - 称为极端解决方案。
Okay.
I have a vector<bool> of fixed size N. Let''s call it bob.
I''m zero''ing bob quite a bit, i.e.:
bob = vector<bool>(N, false);
Naturally, this is quite slow, because of all the object creation.
Can I do any better than the following:
bob.clear(); bob.resize(N, false);
?
I don''t mind changing the container type or the entry type (e.g.
bool->char), so if you have any suggestions in that vein then fire
away.
I''m trying to get this to be really fast, so I''ll entertain any
so-called "extreme solution".
怎么样
memset(& bob [i],0,N * sizeof(bob [i]));
内置类型(或任何POD)?但是你不能用非POD来支付
。
V
How about
memset(&bob[i], 0, N*sizeof(bob[i]));
for built-in types (or any PODs for that matter)? You can''t do it
with non-PODs, however.
V
为了娱乐任何解决方案,我相信这是最快可能的方式0-init聚合POD:
bool bob [123] = {0};
当然如果你已经分配了阵列,你就不能这样做。
小心,
John Dibling
In the interest of entertaining any solution, I believe that this the
fastest possible way to 0-init an aggregate of PODs:
bool bob[123] = {0};
Of course if you already have allocated the array, you can''t do this.
Take care,
John Dibling
这篇关于将矢量归零的最快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!