循环vs memset初始化数组..不同的结果? [英] loop vs memset to initialize array.. different results?

查看:64
本文介绍了循环vs memset初始化数组..不同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello everybdy,

由于以下原因,我有点困惑:


在我的代码中,我使用了一个简单的for循环来初始化一个2D阵列

浮点数为零。由于效率原因,我把它更改为使用

memset,我得到完全不同的结果..这怎么可能?


这是一个例子:


float gaborfilter [filtersize] [filtersize];


memset(gaborfilter,0,sizeof(float)* filtersize * filtersize);





for(int i = 0; i< filtersize; i ++){

for(int j) = 0; j< filtersize; j ++){

gaborfilter [i] [j] = 0;

}

}


后来的计算使用这两种

方法给出完全不同的结果。有没有真正明显不同的东西,我目前缺少的是



非常感谢

Tim

解决方案

silversurfer2025写道:


Hello everybdy,

由于以下原因,我有点困惑:


在我的代码中,我使用了一个简单的for循环来初始化一个2D数组

的浮点数为零。由于效率原因,我把它更改为使用

memset,我得到完全不同的结果..这怎么可能?


这是一个例子:


float gaborfilter [filtersize] [filtersize];


memset(gaborfilter,0,sizeof(float)* filtersize * filtersize);





for(int i = 0; i< filtersize; i ++){

for(int j) = 0; j< filtersize; j ++){

gaborfilter [i] [j] = 0;

}

}


后来的计算使用这两种

方法给出完全不同的结果。有没有真正明显不同的东西,我目前缺少的是b $ b?



A零浮点变量不一定与

所有位零相同。 ''memset''清除所有位。为元素分配0

数组做正确的事(tm)。您是否想过简单地初始化阵列?或者您是否需要定期清除它?


float blah [one] [two] = {}; //将所有元素设置为0


V

-

请在回复时删除大写''A'电子邮件

我没有回复最热门的回复,请不要问




" silversurfer2025" < ki **** @ web.dewrote in message

news:11 ********************** @ 75g2000cwc.googlegro ups.com ...


你好everybdy,

由于以下原因我有点困惑:

在我的代码中,我使用了一个简单的for循环来初始化2D数组

的浮点数为零。由于效率原因,



您是否已通过计时或剖析证明

确实存在效率问题?


我把它更改为使用

memset



不要。
< blockquote class =post_quotes>
我得到完全不同的结果..这怎么可能?



因为0.0的表示不一定是
all-bits-zero。


>

这是一个例子:


float gaborfilter [filtersize] [filtersize];


memset(gaborfilter,0,sizeof(float)* filtersize * filtersize);



这只能保证使用无符号整数类型

(例如''unsigned int'',''unsigned char''。


OR


for(int i = 0; i< filtersize; i ++){<

for(int j = 0; j< filtersize; j ++){

gaborfilter [i] [j] = 0;



这是为''float''对象类型数组中每个元素赋值的唯一可移植方式。

但如果你想要所有值为零,你可以简单地在创建数组时初始化数组:


float gaborfilter [filtersize] [filtersize] = {0};

/ *(所有元素现在的值都为零)* /


}

}


后来的计算使用这两个

方法给出完全不同的结果。是否有一些真正明显不同的东西,我是

目前缺少?



见上文。


另外,你应该使用''double''而不是''浮动''。

甚至有可能类型''double''可以更加有效
(但这最终取决于你的<
platforrm)。


最后,这是C ++,所以你应该使用

容器而不是数组。 />

std :: vector< float>(filtersize,std :: vector< float>(filtersize));


/ *所有元素现在都有值为零。 * /


-Mike




Mike Wahler schrieb:


" silversurfer2025" < ki **** @ web.dewrote in message

news:11 ********************** @ 75g2000cwc.googlegro ups.com ...


你好everybdy,

由于以下原因我有点困惑:

在我的代码中,我使用了一个简单的for循环来初始化2D数组

的浮点数为零。由于效率原因,



您是否已通过计时或剖析证明

确实是一个效率问题?


我把它更改为使用

memset




不要。



OK;)


>


我得到完全不同的结果..这怎么可能?



因为0.0的表示不一定是
all-bits-zero。



这是一个例子:


float gaborfilter [filtersize] [filtersize];


memset( gaborfilter,0,sizeof(float)* filtersize * filtersize);



这只能保证使用无符号整数类型

(例如''unsigned int'',''unsigned char''。



啊,好吧..看得很糟糕...我以为我可以让我的代码工作

更快..


>


OR


for(int i = 0; i< filtersize; i ++){

for(int j = 0; j< filtersize; j ++){

gaborfilter [i] [j] = 0 ;



这是为''float''对象类型数组中每个元素分配值的唯一可移植方式。


但是如果你想让所有值都为零,你可以简单地在创建它时初始化数组:


float gaborfilter [filtersize] [filtersize] = {0};



之前我试过这个,但是我得到编译错误:

错误:变量-s ized对象''gaborfilter''可能不是

初始化

我能做些什么吗?


/ *(所有元素现在的值都为零)* /


}

}


后来的计算使用这两种

方法给出完全不同的结果。有没有真正明显不同的东西,我目前缺少的是b $ b?



见上文。


此外,你应该使用''double''而不是''float''。

甚至有可能类型''double''可以更加有效
(但这最终取决于你的

platforrm)。



我会马上改变它,我认为浮动会更好

内存原因..


>

最后,这是C ++,所以你应该使用

容器而不是数组。



我只使用它们进行计算,结果后来被放入

向量中。 (请不要问!)


>

std :: vector< float>(filtersize,std :: vector< float> (filtersize));


/ *现在所有元素的值都为零。 * /


-Mike



Tim


Hello everybdy,
I am a little confused for the following reason:

In my code I used a simple for-loop in order to initialize a 2D-array
of floats to zero. Because of efficiency reasons, I changed it to use
memset and I get totally different results.. How can this be?

Here is the example:

float gaborfilter[filtersize][filtersize];

memset(gaborfilter, 0, sizeof(float) * filtersize * filtersize);

OR

for(int i = 0; i < filtersize;i++){
for(int j = 0; j < filtersize;j++){
gaborfilter[i][j] = 0;
}
}

Later calculations give totally different results using these two
methods. Is there something really obviously different, which I am
currently missing?

Thanks a lot
Tim

解决方案

silversurfer2025 wrote:

Hello everybdy,
I am a little confused for the following reason:

In my code I used a simple for-loop in order to initialize a 2D-array
of floats to zero. Because of efficiency reasons, I changed it to use
memset and I get totally different results.. How can this be?

Here is the example:

float gaborfilter[filtersize][filtersize];

memset(gaborfilter, 0, sizeof(float) * filtersize * filtersize);

OR

for(int i = 0; i < filtersize;i++){
for(int j = 0; j < filtersize;j++){
gaborfilter[i][j] = 0;
}
}

Later calculations give totally different results using these two
methods. Is there something really obviously different, which I am
currently missing?

A "zero" in a floating point variable is not necessarily the same as
"all bits zero". ''memset'' clears all bits. Assigning 0 to the elements
of the array does THE RIGHT THING(tm). Have you thought about simply
initialising the array or do you need to periodically clear it?

float blah[one][two] = {}; // sets all elements to 0

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask



"silversurfer2025" <ki****@web.dewrote in message
news:11**********************@75g2000cwc.googlegro ups.com...

Hello everybdy,
I am a little confused for the following reason:

In my code I used a simple for-loop in order to initialize a 2D-array
of floats to zero. Because of efficiency reasons,

Have you proven with timing or profiling that there
is indeed an efficiency problem?

I changed it to use
memset


Don''t.

and I get totally different results.. How can this be?

Because the representation of 0.0 isn''t necessarily
all-bits-zero.

>
Here is the example:

float gaborfilter[filtersize][filtersize];

memset(gaborfilter, 0, sizeof(float) * filtersize * filtersize);

This is only guaranteed to work with unsigned integer types
(e.g. ''unsigned int'', ''unsigned char''.

OR

for(int i = 0; i < filtersize;i++){
for(int j = 0; j < filtersize;j++){
gaborfilter[i][j] = 0;

This is the only portable way to assign values to
each element of the array of type ''float'' objects.
But if you want all values to be zero, you could simply
initialize the array when you create it:

float gaborfilter[filtersize][filtersize] = {0};
/* (all elements now have value of zero) */

}
}

Later calculations give totally different results using these two
methods. Is there something really obviously different, which I am
currently missing?

See above.

Also, you should use type ''double'' rather than ''float''.
There''s even a possibility that type ''double'' could be
more ''efficient'' (but this ultimately depends upon your
platforrm).

And finally, this is C++, so imo you should be using
containers rather than arrays.

std::vector<float>(filtersize, std::vector<float>(filtersize));

/* All elements now have values of zero. */

-Mike



Mike Wahler schrieb:

"silversurfer2025" <ki****@web.dewrote in message
news:11**********************@75g2000cwc.googlegro ups.com...

Hello everybdy,
I am a little confused for the following reason:

In my code I used a simple for-loop in order to initialize a 2D-array
of floats to zero. Because of efficiency reasons,


Have you proven with timing or profiling that there
is indeed an efficiency problem?

I changed it to use
memset



Don''t.

OK ;)

>

and I get totally different results.. How can this be?


Because the representation of 0.0 isn''t necessarily
all-bits-zero.


Here is the example:

float gaborfilter[filtersize][filtersize];

memset(gaborfilter, 0, sizeof(float) * filtersize * filtersize);


This is only guaranteed to work with unsigned integer types
(e.g. ''unsigned int'', ''unsigned char''.

Ah, ok.. wat a pitty... I thought that I could get my code to work
faster..

>

OR

for(int i = 0; i < filtersize;i++){
for(int j = 0; j < filtersize;j++){
gaborfilter[i][j] = 0;


This is the only portable way to assign values to
each element of the array of type ''float'' objects.
But if you want all values to be zero, you could simply
initialize the array when you create it:

float gaborfilter[filtersize][filtersize] = {0};

I tried this one before, but I get the compiler error:
error: variable-sized object ''gaborfilter'' may not be
initialized
Anything I can do about it?

/* (all elements now have value of zero) */

}
}

Later calculations give totally different results using these two
methods. Is there something really obviously different, which I am
currently missing?


See above.

Also, you should use type ''double'' rather than ''float''.
There''s even a possibility that type ''double'' could be
more ''efficient'' (but this ultimately depends upon your
platforrm).

I''ll change it right away, I thought that float would be better for
memory-reasons..

>
And finally, this is C++, so imo you should be using
containers rather than arrays.

I am only using them for calculations, the results are later put into a
vector. (please do not ask!)

>
std::vector<float>(filtersize, std::vector<float>(filtersize));

/* All elements now have values of zero. */

-Mike

Tim


这篇关于循环vs memset初始化数组..不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆