简体   繁体   English

删除与新实例

[英]Remove vs New Instance

I'm wondering what a better practice is in general .NET programming with class instances that contain a Remove method. 我想知道在具有包含Remove方法的类实例的常规.NET编程中,有什么更好的做法。

The question is: if I have a StringBuilder sb with some data, is it smarter to use the .Remove(0, sb.Length) or to create a new instance and leave the old to the GC to collect. 问题是:如果我有一个包含一些数据的StringBuilder sb,使用.Remove(0,sb.Length)还是创建一个新实例并将旧实例留给GC收集是否更聪明? There are quite a few factors that I am aware of so I would like to know your opinions on this one. 我知道很多因素,因此我想了解您对此的看法。

Thanks, 谢谢,

Sanjin 三进

I would definitely go for creating a new instance until a profiler has shown that this is the most critical part of your application. 在探查器显示这是应用程序中最关键的部分之前,我肯定会创建一个新实例。

This micro optimization kind of reuse just confuses any readers about the intent of the code. 这种微优化的重用类型只会使任何读者对代码的意图感到困惑。

In general, create a new one. 通常,创建一个新的。

The .NET memory system is geared toward creating and de-allocating lots of small, short-lived objects quickly. .NET内存系统旨在快速创建和取消分配许多小的短期对象。 StringBuilder.Remove() is likely to be slower. StringBuilder.Remove()可能会更慢。

For very large data (over 80kB) the rules change a little, depending on lots of factors. 对于非常大的数据(超过80kB),规则会有所变化,具体取决于许多因素。

Don't use .Remove(0, sb.Length) use .Clear() instead. 不要使用.Remove(0, sb.Length) .Clear()而是使用.Clear() It's easier to read and if Remove hasn't been optimized for this special use-case Clear will be faster too. 它更易于阅读,并且如果尚未针对此特殊用例对Remove进行优化,那么Clear也会更快。
Or just allocate a new StringBuilder. 或者只是分配一个新的StringBuilder。 The StringBuilder itself it lightweight, so I don't think using a new one will be expensive. StringBuilder本身很轻巧,因此我认为使用新的不会昂贵。
I usually wouldn't distinguish Clear() and allocating a new StringBuilder based on performance benefits of one, but on which creates the better readable code. 我通常不会区分Clear()并基于一个的性能优势分配一个新的StringBuilder,但是在此基础上会创建更好的可读性代码。 And that depends on your use-case. 这取决于您的用例。 Don't micro optimize unless you're profiler has shown that it's necessary. 除非您的分析器表明有必要,否则请不要进行微优化。
In my experience you allocate a new StringBuilder at the beginning of some method and only call ToString when producing the return-value of the function. 以我的经验,您在某些方法的开头分配了一个新的StringBuilder,并且仅在生成函数的返回值时才调用ToString In that case it's stupid to complicate the function interface just to reuse a StringBuilder. 在那种情况下,仅仅重复使用StringBuilder会使函数接口复杂化是愚蠢的。

And I think when you call ToString() the StringBuilder gives out it's internal, so the internal buffer becomes immutable and StringBuilder needs to allocate a new on the next change one anyways. 而且我认为,当您调用ToString() ,StringBuilder会发出它的内部信息,因此内部缓冲区变得不可变,并且StringBuilder仍然需要为下一个更改分配一个新值。

One micro-optimization that can be useful is passing in a capacity to the constructor of a StringBuilder, if you know exactly(or at least a lower bound on) how long the result will be. 可能有用的一种微优化是将一个容量传递给StringBuilder的构造函数,如果您确切知道(或至少是下限)结果将持续多长时间。 Then it doesn't have to grow the array in multiple steps. 这样就不必分多个步骤扩展数组。 For example if you know that the output will be at least 10000 characters you can init the builder to a capacity of 10000. 例如,如果您知道输出将至少为10000个字符,则可以将构建器初始化为10000个容量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM