为什么字符串在许多编程语言中是不可变的? [英] Why are strings immutable in many programming languages?

查看:140
本文介绍了为什么字符串在许多编程语言中是不可变的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:



Possible Duplicate:
Why can't strings be mutable in Java and .NET?
Why .NET String is immutable?

有几个问题可以解决,但问题/ 2365272 / why-net-string-is-immutable>为什么.NET String是不可变的?语言为此选择了,如C#,Java和Python。如果想要像 compare 这样的操作节省内存或提高效率,对连接和其他修改操作有什么影响?

Several languages have chosen for this, such as C#, Java, and Python. If it is intended to save memory or gain efficiency for operations like compare, what effect does it have on concatenation and other modifying operations?

推荐答案

不可变类型通常是一件好事:

Immutable types are a good thing generally:


  • 锁定不能改变的东西!)

  • 它们减少错误:可变对象容易被改变,当你不期望它可以引入各种奇怪的错误

  • 它们可以安全地共享(即对同一对象的多个引用),这可以减少内存消耗并提高缓存利用率。

  • 如果您必须获取可变对象的防御副本,共享还会使复制非常便宜的O(1)操作,如果它是O(n)。这是一个很大的事情,因为复制是一个令人难以置信的常见操作(例如,每当你想传递参数....)

  • They work better for concurrency (you don't need to lock something that can't change!)
  • They reduce errors: mutable objects are vulnerable to being changed when you don't expect it which can introduce all kinds of strange bugs ("action at a distance")
  • They can be safely shared (i.e. multiple references to the same object) which can reduce memory consumption and improve cache utilisation.
  • Sharing also makes copying a very cheap O(1) operation when it would be O(n) if you have to take a defensive copy of a mutable object. This is a big deal because copying is an incredibly common operation (e.g. whenever you want to pass parameters around....)

一些语言(特别是Haskell和Clojure之类的函数式语言)甚至更进一步,使得一切都变得不可改变。如果您对不变性的好处感兴趣,则此启发式视频非常值得一看

Some languages (particularly functional languages like Haskell and Clojure) go even further and make pretty much everything immutable. This enlightening video is very much worth a look if you are interested in the benefits of immutability.

不可变类型有一些小的缺点:

There are a couple of minor downsides for immutable types:


  • 操作创建一个更改的字符串,如串联更昂贵,因为你需要构造新的对象。通常,用于连接两个不可变字符串的成本是O(n + m),虽然如果使用基于树的字符串数据结构,如a Rope 。此外,您可以随时使用特殊工具,例如Java的 StringBuilder (如果您真的需要)

  • 对大字符串进行小改动可能导致需要构造大型String的完全新副本,这显然会增加内存消耗。注意,这通常不是垃圾收集语言中的一个大问题,因为如果你不保存引用,旧的副本会很快收集垃圾收集。

  • Operations that create a changed string like concatenation are more expensive because you need to construct new objects. Typically the cost is O(n+m) for concatenating two immutable Strings, though it can go as low as O(log (m+n)) if you use a tree-based string data structure like a Rope. Plus you can always use special tools like Java's StringBuilder if you really need to concatenate Strings efficiently.
  • A small change on a large string can result in the need to construct a completely new copy of the large String, which obviously increases memory consumption. Note however that this isn't usually a big issue in garbage-collected languages since the old copy will get garbage collected pretty quickly if you don't keep a reference to it.

总的来说,不变性的优点远远超过了最小的缺点。即使你只对性能感兴趣,并发优势和复制的廉价性通常会使不可变字符串比具有锁定和防御复制的可变字符串更加高效。

Overall though, the advantages of immutability vastly outweigh the minor disadvantages. Even if you are only interested in performance, the concurrency advantages and cheapness of copying will in general make immutable strings much more performant than mutable ones with locking and defensive copying.

这篇关于为什么字符串在许多编程语言中是不可变的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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