Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

As I understand it, writing your objects in an immutable style can help the performance of your program for compiled languages, as the compiler tends to rewrite your code in single static assignment form under the hood anyway.

Is there a performance benefit to using immutable objects in an interpreted language like Python or PHP?

I understand there may be readability or code quality benefits from immutability, but I am asking specifically about performance.

share|improve this question
Any sources for the reasoning in your first paragraph? I don't doubt GHC and friends does explot immutability for all it's worth, but I'd be surprised if that blanket statement applies to all compilers. – delnan Mar 14 '12 at 16:12
No source, just what I remember from compilers class.. – Kevin Burke Mar 14 '12 at 16:15
up vote 6 down vote accepted

In CPython, allocation of tuples (basically immutable lists) can be slightly faster than an allocation of the equivalent mutable type with the same items. I vaguely remember similar rumors about immuable sets, but timeit didn't confirm it. Tuples can also be smaller than lists, from a few bytes to 50% (if you hit a spot where the list had to resize, and doubled its capacity to speed up future growth). Moreover, tuples and sets are - in some contexts - subject to peephole optimizations which allow creating the object once and re-using it instead of re-building it every time.

But that's mostly peanuts. It's not why we use these types, at least usually. If ever, these differences are only invoked when seriously optimizing. It's not an aspect I usually consider when deciding for either, especially since there are other benefits, not to mention the enormous semantic impact.

share|improve this answer

The major benefit of immutable variables come the moment you go distributed and have your data spread over several computers. Mutable data needs elaborate mechanisms to ensure that when data is updated, all copies in various caches are updated too, which besides being elaborate also take time.

Immutable data can be copied and cached as much as is needed. You are guaranteed that the data will not change.

I do not know if Jython supports multi-threading, if so, it would be an advantage on heavily multithreaded, multi-core applications.

share|improve this answer

In Python there is nothing in the language itself to mark an object as immutable so no hints can be given to the interpreter for optimization.

If the process is threaded and the object has "protected" properties then programmers don't need to worry about synchronization when using that object so there could be a performance gain.

share|improve this answer
I'm confused by your answer, because python does indeed have immutable objects. What does "mark[ing] an object" have to do with the use of existing immutable objects in python? – Bryan Oakley Mar 15 '12 at 12:18
There are immutable built in objects. I am talking about user defined objects. There is no flag or other method to indicate to the interpreter that the user defined object is immutable thus allowing optimizations to occur. – Lance Helsten Mar 15 '12 at 12:44

in Python and PHP there's very little benefit, most of it because immutable structures are usually simpler and smaller because they don't have to support any modifying feature.

OTOH, modern JITs (like LuaJIT or any modern JavaScript) could take advantage of any opportunity to constrain possibilities and produce tighter code (similar to what compilers do, but way more aggressively).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.