String.IsInterned的作用是什么? [英] What is the purpose of String.IsInterned?

查看:41
本文介绍了String.IsInterned的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String 类中,有一个方法 IsInterned().我从不使用这种方法.请帮助我了解此方法的最佳用法.

In the String class there is a method IsInterned(). I never use this method. Please help me to understand the best uses of this method.

推荐答案

考虑到实习是一种优化;它以牺牲某些理想品质为代价,而以其他品质为代价.特别是,实习生具有以下良好素质:

Consider that interning is an optimisation; it trades a decrease in some desirable qualities for an increase in others. In particular interning has the following good qualities:

  1. 内存不会浪费在重复的字符串上.
  2. 已知被拘禁的字符串之间的相等比较非常快.
  3. 碰巧相交的字符串之间的平等比较仍然比不相交的字符串快得多.
  4. 在某些情况下,其他比较可以提高性能.

它具有以下不良品质:

  1. 不是很频繁地(如果有的话)对字符串进行垃圾回收,因此可以回收的内存用于不再出现或很长时间的字符串.(实习所有的字符串,最终可能会导致真正令人讨厌的内存使用.)

作为一种优化,我们在好品质胜过坏品质或不适合不良品质的情况下使用它(如果我们知道字符串在应用程序的整个生命周期中都会存在,或者知道它会被使用很多次,那么坏的部分就不成立了.

As an optimisation, we use it where the either the good qualities out-weigh the bad or where the bad qualities don't hold (if we know the string is going to be around for the lifetime of the application anyway, or know it will be used many times, then the bad part doesn't hold).

出于同样的原因,我们也不会在劣质胜过良品的地方使用它.(大部分时间).

And by the same token we don't use it where the bad qualities will out-weigh the good. (Most of the time).

IsInterned()可用于查找路口点.

考虑到我有一个字符串属性 Name :

Consider I have a string property Name:

public string Name { get; set; }

比方说,我知道通常会查找具有给定 Name 的对象,或者尝试查找具有相同 Name 的对象,或者进行很多相等的操作比较就可以了.或者说我知道会有很多其他具有相同 Name 的对象.或两者皆有.

Let's say I know that it's common to look for objects with a given Name, or to try to find objects with the same Name or otherwise do a lot of equality comparisons on it. OR Let's say I know there will be a lot of other objects with the same Name. OR Both.

在这些情况下,我可能会考虑实习:

In these cases I might consider interning:

private string _name;
public string Name
{
  get { return _name; }
  set { _name = string.Intern(value); }
}

当然,这是一个好主意还是取决于上面提到的实习的好坏.

Of course, whether this was a good idea or not depends on the good and bad qualities of interning mentioned above.

在使用和不使用之间的可能性是:

In-between using and not using is the possibility:

private string _name;
public string Name
{
  get { return _name; }
  set { _name = string.IsInterned(value) ?? value; }
}

在这里,如果字符串 value 已经被实习,那么实习的不利方面已经在起作用,我们不再遭受任何痛苦,因此我们可以利用它.但是,如果尚未对 value 进行赋值,那么我们就可以按原样使用它.

Here if the string value is already interned then we the down-sides of interning are already at work and we don't suffer any more, so we take advantage of it. But if value is not already interned then we just use it as-is.

这也是一种优化,针对不同的情况进行了优化.仅当看到的合理数量的值可能被某些其他代码插入(或因为它们与程序集中的文字匹配)时,它才有好处,否则只会浪费时间进行查找.它可能不如 Intern()有用,后者反过来比仅使用字符串和忽略interning有用的少,但这确实显示了它可能有用的时间.

This is also an optimisation, that optimises for a different case. It only benefits if a reasonable number of the values seen are likely to be interned by some other code (or because they match literals in the assembly), otherwise it just wastes time doing lookups. It's probably less often useful than Intern() which in turn is less often useful than just using the strings and ignoring interning, but that does show a time when it could be useful.

这篇关于String.IsInterned的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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