这是更好的清除还是新的? [英] It's Better CLEAR or NEW ?

查看:69
本文介绍了这是更好的清除还是新的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想听听你关于最佳编程的*意见和建议*

在.NET下练习。


鉴于有几次我们无法改变:


MyCollection.Clear进入新MyCollection的实例化


,因为我们为孤儿提供了一些必要的参考,


我也注意到我可以等同地多次。决定是否清算和重复使用
集合

(或任何其他类别),或者创建一个新的




这通常发生在具有类范围的集合中。


在这种情况下,它在功能上是等效的,我可以

选择哪个更合适?


清除一个集合并重复使用它会更好。或者只是实例化一个

全新的一个将旧的

一个留给GC?


我的意思是为了记忆的目的用法,速度,GC等。


-P


I would like to hear your *opinion and advice* on best programming
practice under .NET.

Given that several time we cannot change:

MyCollection.Clear into the instantiation of a NEW MyCollection

because we make orphan some needed reference,

I notice also that several times I can "equivalently" decide whether a
collection
(or any other class) be cleared and reused or a NEW one should be
created.

This usually happens with collections having a class scope.

In such cases, where it is functionally "equivalent", and I can
choose, what is more advisable ?

It''s better to clear a collection and reuse it. Or just instantiate a
brand new one leaving the old
one to the GC ?

I mean to the purpose of memory usage, speed, GC, etc.

-P

推荐答案

在文章< ; 11 ********************** @ r29g2000hsg.googlegroups .com>,
pa *********** @ libero.it 说...
In article <11**********************@r29g2000hsg.googlegroups .com>,
pa***********@libero.it says...

>

我想听听你关于最佳编程的*意见和建议*

在.NET下练习。


鉴于有几次我们无法改变:


MyCollection.Clear进入新MyCollection的实例化


,因为我们为孤儿提供了一些必要的参考,


我也注意到我可以等同地多次。决定是否清算和重复使用
集合

(或任何其他类别),或者创建一个新的




这通常发生在具有类范围的集合中。


在这种情况下,它在功能上是等效的,我可以

选择哪个更合适?


清除一个集合并重复使用它会更好。或者只是实例化一个

全新的一个将旧的

一个留给GC?


我的意思是为了记忆的目的用法,速度,GC等。
>
I would like to hear your *opinion and advice* on best programming
practice under .NET.

Given that several time we cannot change:

MyCollection.Clear into the instantiation of a NEW MyCollection

because we make orphan some needed reference,

I notice also that several times I can "equivalently" decide whether a
collection
(or any other class) be cleared and reused or a NEW one should be
created.

This usually happens with collections having a class scope.

In such cases, where it is functionally "equivalent", and I can
choose, what is more advisable ?

It''s better to clear a collection and reuse it. Or just instantiate a
brand new one leaving the old
one to the GC ?

I mean to the purpose of memory usage, speed, GC, etc.



只需创建一个新的(假设你的收藏品中的元素不是

持有任何非托管资源。


当您开发应用程序时,请继续测试它以获得所需的性能

基准测试(速度,内存使用等)...... )等到你开始

实际上看到了修复它的问题。


-

Patrick Steele(pa ** ***@mvps.org)
http://weblogs.asp.net/ psteele


Patrick Steele写道:
Patrick Steele wrote:

在开发应用程序时,请继续测试它你期望的性能

基准测试(速度,内存使用等等)等到你开始

实际上看到了修复它的问题。
As you develop your app, keep testing it for your desired performance
benchmarks (speed, memory usage, etc...) Wait until you start to
actually see a problem to fix it.



我不能再多说了,但那只是我。我的目标是建立

的性能和质量,而不是测试它。再说一次,我主要是为Pocket PC和Smartphone写的,所以我真的要思考关于性能。

然而,''测试''性能和/或质量恕我直言不是正确的方式。


Pamela,为什么不''你运行一些测试(只需几行)并报告回原来这个组的速度快了多少。另外,如果这个clear / new在内部循环中,那么清楚会更好,但是忽略了

内存分配和拼图的垃圾收集部分,速度差异

将是一个很好的信息。


谢谢,


希尔顿

I couldn''t disgree more, but that''s just me. My goal is to build in
performance and quality, not test it in. Then again, I write primarily for
the Pocket PC and Smartphone, so I really have to think about performance.
However, ''testing-in'' performance and/or quality IMHO is not the right way.

Pamela, why don''t you run some tests (just a few lines) and report back to
the group which was faster and by how much. Also, if this clear/new is in
an inner loop, it seems logical that clear would be better, but ignoring the
memory allocation and garbage collection piece of the puzzle, the speed diff
would be good info.

Thanks,

Hilton


Hilton< no **** @ nospam.comwrote:
Hilton <no****@nospam.comwrote:

Patrick Steele写道:
Patrick Steele wrote:

在开发应用程序时,请继续测试它以获得所需的性能

基准测试(速度,内存使用等等)等到你开始使用

实际上看到了修复它的问题。
As you develop your app, keep testing it for your desired performance
benchmarks (speed, memory usage, etc...) Wait until you start to
actually see a problem to fix it.



我不能再多说了,但那只是我。我的目标是建立

的性能和质量,而不是测试它。再说一次,我主要是为Pocket PC和Smartphone写的,所以我真的要思考关于性能。

然而,''测试''性能和/或质量恕我直言不是正确的方法。


I couldn''t disgree more, but that''s just me. My goal is to build in
performance and quality, not test it in. Then again, I write primarily for
the Pocket PC and Smartphone, so I really have to think about performance.
However, ''testing-in'' performance and/or quality IMHO is not the right way.



我和Patrick在一起。

瓶颈常常出现在意想不到的地方时,微优化*所有*

代码(通常会在同一点上降低可读性)没有什么意义。




http://en.wikipedia.org/wiki/Optimiz...cience)#Quotes


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http: //www.msmvps.com/jon.skeet

如果回复该群组,请不要给我发邮件

I''m with Patrick. There''s little point in micro-optimising *all* the
code (and often making it less readable at the same point) when
bottlenecks are often in unexpected places.

See

http://en.wikipedia.org/wiki/Optimiz...cience)#Quotes

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


这篇关于这是更好的清除还是新的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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