重新初始化一个数组 [英] reinitialization an array

查看:134
本文介绍了重新初始化一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好


我正在开展一个项目,在这个项目中,代码优化起到了获得所需性能的重要作用。

我想知道是否有可能通过一个声明重新初始化

数组大到零。

例如

main()

{

int i = 0;

char A [30];

while(condition )

{

//开始

for(i = 0; i< 30; i ++)

{

A [i] = 0;

}

//结束


for( i = 0; i <30; i ++)

{

printf(在索引%d中输入一个值:\ n,i);

scanf("%d",& A [i]);

}

/ *后跟几个其他陈述* /

} //结束时间

}


来自上面的代码我要删除for inbetween // start和/ /

结束并替换为单个语句(不应该是

函数)这会将A内容分配给零。或者是否有任何其他

方法可以减少时间复杂度。


等待回复

Raghu

解决方案

5月30日,11:19 * am,raghu< ragavaku ... @ gmail.comwrote:


你好


* * * *我正在开发一个项目,在这个项目中,代码优化起到了重要的作用。所需的性能。

我想知道是否有可能通过一个声明重新初始化

数组大到零。

例如

main()

{

* * * * int i = 0;

* * * * char A [30];

while(条件)

{

//开始

for(i = 0; i< 30; i ++)

{

* * A [i] = 0;}


//结束


* * * * for(i = 0; i< 30; i ++)

* * * {

* * * * * printf("输入一个值)指数%d:\ n",i);

* * * * * scanf("%d",& A [i]);

* * *}

/ *后跟其他一些陈述* /


} //结束时间

} //中的// start *和//

结束并替换为单个语句(不应该是

函数)将A内容分配给零。或者是否有任何其他

方法可以减少时间复杂度。


等待回复

Raghu



Hello Raghu,



#include< string .h>

memset(A,0,sizeof(A));


我希望这应该可行:)


2008年5月29日星期四下午11:19:06 -0700,raghu写道:


你好


我正在开发一个项目,在这个项目中,代码优化起到了获得所需性能的重要作用。

我想知道有没有可能的使用单个语句重新初始化一个大到零的
数组。



你可以做(​​至少)两件事。


1)将数组声明放在一个块内(在这种情况下阻止)

并给一个初始化者。

char A [30] = {0};

这将设置所有元素为零(不只是第一个),每当

执行路径进入块时。


2)因为你有一个char数组,你可以做一个memset(A,0,sizeof(A)),

这相当于你的for循环:


for (i = 0; i <30; i ++)

{

A [i] = 0;

}



(如果你有非整数类型,则不再保证这种等价。)

(如果你收到A作为函数参数,那么请注意sizeof(A))


使用其中任何一个可能会或可能不会提高性能,但会简化源代码。测量时间并检查是否归零

arry是实际的瓶颈或其他东西。 (使用分析器,

如果你还没有使用过。)


Szabolcs


< blockquote> 5月30日上午11点35分,Szabolcs Borsanyi< s.borsa ... @sussex.ac.uk>

写道:


2008年5月29日星期四,下午11:19:06 -0700,raghu写道:


你好


我正在开展一个项目,在这个项目中,代码优化起到了获得所需性能的重要作用。

我想知道有没有可能用一个语句重新初始化一个大到零的数组到零。



你可以做(​​至少)两件事。


1)将数组声明放在一个块内(在这种情况下阻止)

并给一个初始化者。

char A [30] = {0};

这将设置所有元素为零(不只是第一个),每当

执行路径进入块时。


2)因为你有一个char数组,你可以做一个memset(A,0,sizeof(A)),

这相当于你的for循环:for(i = 0; i< 30; i ++)


{

A [i] = 0;

}



(如果你有非整数类型,则不再保证这种等价。)

(如果你收到A作为函数参数,那么请注意sizeof(A))


使用其中任何一个可能会也可能不会提高性能,但是会简化源代码。测量时间并检查是否归零

arry是实际的瓶颈或其他东西。 (使用分析器,

如果你还没有使用过。)


Szabolcs



但是memset是一个执行相同操作的函数,但它会花费一些时间来执行

,如函数调用,堆栈分配等。还有其他吗? br />
方法没有调用函数。


Hello

I am working on a project where code optimization plays a
vital role to get the required performance.
I would like to know is there any possible to reinitialization an
array of big size to zero with a single statement.
For example
main()
{
int i =0;
char A[30];
while(condition)
{
//Start
for( i = 0; i < 30; i++)
{
A[i] = 0;
}
//End

for(i = 0; i < 30; i++)
{
printf(" enter a value in the index %d:\n",i);
scanf("%d", &A[i]);
}
/*followed by few other statements*/
} //End of while
}

from the above code I want to remove the for inbetween //start and //
End and replace with a single statement(which should not be a
function) which will assign A contents to zero. Or is there any other
approach that will reduce the time complexity than this.

waiting for reply
Raghu

解决方案

On May 30, 11:19*am, raghu <ragavaku...@gmail.comwrote:

Hello

* * * * I am working on a project where code optimization plays a
vital role to get the required performance.
I would like to know is there any possible to reinitialization an
array of big size to zero with a single statement.

For example
main()
{
* * * * int i =0;
* * * * char A[30];
while(condition)
{
//Start
for( i = 0; i < 30; i++)
{
* * A[i] = 0;}

//End

* * * *for(i = 0; i < 30; i++)
* * * {
* * * * * printf(" enter a value in the index %d:\n",i);
* * * * * scanf("%d", &A[i]);
* * * }
/*followed by few other statements*/

} //End of while
}

from the above code I want to remove the for inbetween //start *and //
End and replace with a single statement(which should not be a
function) which will assign A contents to zero. Or is there any other
approach that will reduce the time complexity than this.

waiting for reply
Raghu

Hello Raghu,

try using memset ..

#include <string.h>
memset(A, 0, sizeof(A));

I hope this should work :)


On Thu, May 29, 2008 at 11:19:06PM -0700, raghu wrote:

Hello

I am working on a project where code optimization plays a
vital role to get the required performance.
I would like to know is there any possible to reinitialization an
array of big size to zero with a single statement.

There are (at least) two things you can do.

1) place the array declaration inside of a block (while block in this case)
and give an initialiser.
char A[30]={0};
This will set all the elements to zero (not just the first one), whenever
the path of execution enters the block.

2) since you have a char array, you can do a memset(A,0,sizeof(A)),
which is just equivalent to your for loop:

for( i = 0; i < 30; i++)
{
A[i] = 0;
}

(If you had non-integer types, this equivalence is no longer guaranteed.)
(If you receive A as a function argument, then watch out with sizeof(A))

Using either of these may or may not improve the performance, but will
simplify the source code. Measure the times and check if zeroing out
an arry is the actual bottleneck or some something else. (Use a profiler,
if you have not already been using one yet.)

Szabolcs


On May 30, 11:35 am, Szabolcs Borsanyi <s.borsa...@sussex.ac.uk>
wrote:

On Thu, May 29, 2008 at 11:19:06PM -0700, raghu wrote:

Hello

I am working on a project where code optimization plays a
vital role to get the required performance.
I would like to know is there any possible to reinitialization an
array of big size to zero with a single statement.


There are (at least) two things you can do.

1) place the array declaration inside of a block (while block in this case)
and give an initialiser.
char A[30]={0};
This will set all the elements to zero (not just the first one), whenever
the path of execution enters the block.

2) since you have a char array, you can do a memset(A,0,sizeof(A)),
which is just equivalent to your for loop:for( i = 0; i < 30; i++)

{
A[i] = 0;
}


(If you had non-integer types, this equivalence is no longer guaranteed.)
(If you receive A as a function argument, then watch out with sizeof(A))

Using either of these may or may not improve the performance, but will
simplify the source code. Measure the times and check if zeroing out
an arry is the actual bottleneck or some something else. (Use a profiler,
if you have not already been using one yet.)

Szabolcs

But memset is a function which will perform same operation but it will
take some time to execute
like function calling, stack allocation etc. Is there any other
approach with out calling the function.


这篇关于重新初始化一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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