零阵列的快速方法 [英] Quick way to zero array

查看:68
本文介绍了零阵列的快速方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

男人,那个头衔应该在一首诗中,但无论如何......所以,我有这个

程序,它有一个包含4000万个元素的数组。问题是

,我有一个for循环,不断地对这个数组做什么,

,每次迭代,我都要把数组归零。这就是我现在这样做的方式://将lnscores归零(count = 0; count<

chunksize; count ++)lnscores [count] = 0 ;有没有更快的方式

这样做?我知道必须要有一个技巧,因为这个阵列是一个很大的内存大块的内存吗?

解决方案

< blockquote> memset(lnscores,0,chunksize)


ro ********** @ gmail.com 认为:

男人,那个头衔应该在一首诗中,但无论如何......那么,我有这个
程序,它有一个包含4000万个元素的数组。问题是我有一个for循环不断地对这个数组做什么,
并且对于每次迭代,我必须将数组清零。这就是我目前正在做的事情://将lnscores清零(count = 0; count<
chunksize; count ++)lnscores [count] = 0;有没有更快的方法来做到这一点?我知道必须有一个技巧,因为这个阵列是一个相当大的连续内存块吗?




如果你的阵列有文件范围你就不会甚至必须自己归零。

所有这些变量在启动时都归零。否则,声明它并且

初始化:


int lnscores [WHATEVER_SIZE] = {0};


如果你之后必须重新归零,你可以使用`memset()`。


-

世界被征服,星系被摧毁 - 但是一个女人永远是一个女人。

- 柯克,国王的良心,stardate unknown


< ; http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>


2006-05-12, ro ********** @ gmail.com < ro ********* *@gmail.com>写道:

男人,那个标题应该在一首诗中,但无论如何......所以,我有这个
程序,它有一个包含4000万个元素的数组。问题是我有一个for循环不断地对这个数组做什么,
并且对于每次迭代,我必须将数组清零。这就是我目前正在做的事情://将lnscores清零(count = 0; count<
chunksize; count ++)lnscores [count] = 0;有没有更快的方法来做这个?我知道必须有一个技巧,因为这个数组是一个很大的连续内存块吗?




memset(lnscores,0,chunksize * sizeof lnscores [ 0]);


假设0代表0 ... lnscores是什么类型的?


Man, that title should be in a poem, but anyways...So, I have this
program, and it has an array of 40 million elements. The problem is
that I have a for loop that continually is doing stuff to this array,
and for each iteration, I have to zero out the array. This is how I am
currently doing it: // Zero out the lnscores for( count=0; count <
chunksize; count++ ) lnscores[count] = 0; Is there no quicker way
to do this? I know there must be a trick since this array is one big
contiguous chunk of memory right?

解决方案

memset( lnscores, 0, chunksize )


ro**********@gmail.com opined:

Man, that title should be in a poem, but anyways...So, I have this
program, and it has an array of 40 million elements. The problem is
that I have a for loop that continually is doing stuff to this array,
and for each iteration, I have to zero out the array. This is how I
am
currently doing it: // Zero out the lnscores for( count=0; count <
chunksize; count++ ) lnscores[count] = 0; Is there no quicker
way to do this? I know there must be a trick since this array is one
big contiguous chunk of memory right?



If your array has file scope you don''t even have to zero it yourself.
All such variables get zeroed at startup. Otherwise, declare it and
initialise thus:

int lnscores[WHATEVER_SIZE] = {0};

If you have to re-zero it afterwards, you could use `memset()`.

--
Worlds are conquered, galaxies destroyed -- but a woman is always a
woman.
-- Kirk, "Conscience of the King", stardate unknown

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>


On 2006-05-12, ro**********@gmail.com <ro**********@gmail.com> wrote:

Man, that title should be in a poem, but anyways...So, I have this
program, and it has an array of 40 million elements. The problem is
that I have a for loop that continually is doing stuff to this array,
and for each iteration, I have to zero out the array. This is how I am
currently doing it: // Zero out the lnscores for( count=0; count <
chunksize; count++ ) lnscores[count] = 0; Is there no quicker way
to do this? I know there must be a trick since this array is one big
contiguous chunk of memory right?



memset(lnscores, 0, chunksize * sizeof lnscores[0]);

Assuming 0 is represented by 0... what type is lnscores?


这篇关于零阵列的快速方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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