没有循环的初始化 [英] Initialization without loop

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

问题描述

有没有办法创建一个新数组,并将每个数组初始化为

0,但保持此过程的顺序为O(1)?


(通常,我们通过使用for循环进行初始化。即O(n)。)

Is there a way to create a new array, and initialise every of them to
0, but maintain the order to this process to O(1)?

(usually, we do initialisation by using for loop. that is of O(n).)

推荐答案

" caijunfu" < CA ****** @ hotmail.com>在消息中写道

news:bf ************************** @ posting.google.c om ...
"caijunfu" <ca******@hotmail.com> wrote in message
news:bf**************************@posting.google.c om...
有没有办法创建一个新数组,并将每个数组初始化为
0,但保持此过程的顺序为O(1)?
(通常,我们通过使用for循环进行初始化。这是O(n)。)
Is there a way to create a new array, and initialise every of them to
0, but maintain the order to this process to O(1)?

(usually, we do initialisation by using for loop. that is of O(n).)




我不相信有任何方法可以做到这一点'保证是O(1)。你

可以做这样的事情


int array [100] = {0};


那会使用100个零初始化数组(因为初始化列表比元素少了
元素,最后99个元素被赋予相同的值

如果它们是静态存储将获得--ie,0)。使用你的C

实现,初步过程可能最终成为订单

O(1),就我所知。但同样,C标准也无法保证。


问候,


Russell Hanneken
rh ******* @ pobox.com


caijunfu写道:
caijunfu wrote:
有没有办法创建一个新数组,并将每个数组初始化为
0,但保持此过程的顺序为O(1)?

(通常,我们通过使用for循环进行初始化。这是O(n)。)
Is there a way to create a new array, and initialise every of them to
0, but maintain the order to this process to O(1)?

(usually, we do initialisation by using for loop. that is of O(n).)



在许多实现中,memset()会选择最快的方式

初始化。然后,它应该比O(n)更好,达到一定的大小,

可能与缓存行一样大。

calloc()可能会实现如果您的图书馆的开发人员已经照顾了

,那么您已经描述过的最好的通用算法是

任务。您可以为您的平台测试各种备用库。我不知道你是说整个过程的速度,还是只是初始化部分的速度。

- -

Tim Prince


In many implementations, memset() would choose the fastest way of
initialization. Then, it should be better than O(n), up to some size,
perhaps as big as a cache line.
calloc() would be expected to implement the best general algorithm for the
task you have described, if the developers of your library have taken care
to do so. You could test various alternate libraries for your platform. I
don''t know whether you mean the speed of the entire process, or just the
speed of the initialization part.
--
Tim Prince


" Tim Prince" < TI *********** @ xxxxsbcglobal.net>在消息中写道

news:XB ******************** @ newssvr14.news.prodigy .com ...
"Tim Prince" <ti***********@xxxxsbcglobal.net> wrote in message
news:XB********************@newssvr14.news.prodigy .com...
caijunfu写道:
caijunfu wrote:
有没有办法创建一个新数组,并将每个数组初始化为
0,但保持此过程的顺序为O( 1)?

(通常,我们通过使用for循环进行初始化。即O(n)。)
Is there a way to create a new array, and initialise every of them to
0, but maintain the order to this process to O(1)?

(usually, we do initialisation by using for loop. that is of O(n).)


在许多实现中,memset()会选择最快的初始化的方式。然后,它应该比O(n)更好,达到一定的大小,也许和缓存行一样大。
calloc()应该为
你所描述的任务,如果你的库的开发人员已经小心这样做了。


In many implementations, memset() would choose the fastest way of
initialization. Then, it should be better than O(n), up to some size,
perhaps as big as a cache line.
calloc() would be expected to implement the best general algorithm for the
task you have described, if the developers of your library have taken care
to do so.



memset可以给数组中的每个字节赋值(unsigned char)0。

如果你有一组无符号字符,那就没问题了,但如果你有一个数组

,比如说,整数,设置每一个byte to 0并不保证每个

元素的值为0.


calloc将每一位设置为0,但是再次,这可能不会给出每个元素a

的值为0.值0可以表示也可以不表示所有位为零。

:-\

问候,


Russell Hanneken
rh *** ****@pobox.com


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

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