Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1050203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:44:06+00:00 2026-05-16T16:44:06+00:00

Why are mutable strings slower than immutable strings? EDIT: >>> import UserString … def

  • 0

Why are mutable strings slower than immutable strings?

EDIT:

>>> import UserString
... def test():
...     s = UserString.MutableString('Python')
...     for i in range(3):
...         s[0] = 'a'
... 
... if __name__=='__main__':
...     from timeit import Timer
...     t = Timer("test()", "from __main__ import test")
...     print t.timeit()
13.5236170292



>>> import UserString
... def test():
...     s = UserString.MutableString('Python')
...     s = 'abcd'
...     for i in range(3):
...         s = 'a' + s[1:]
... 
... if __name__=='__main__':
...     from timeit import Timer
...     t = Timer("test()", "from __main__ import test")
...     print t.timeit()
6.24725079536


>>> import UserString
... def test():
...     s = UserString.MutableString('Python')
...     for i in range(3):
...         s = 'a' + s[1:]
... 
... if __name__=='__main__':
...     from timeit import Timer
...     t = Timer("test()", "from __main__ import test")
...     print t.timeit()
38.6385951042

i think it is obvious why i put s = UserString.MutableString(‘Python’) on second test.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:44:07+00:00Added an answer on May 16, 2026 at 4:44 pm

    In a hypothetical language that offers both mutable and immutable, otherwise equivalent, string types (I can’t really think of one offhand — e.g., Python and Java both have immutable strings only, and other ways to make one through mutation which add indirectness and therefore can of course slow things down a bit;-), there’s no real reason for any performance difference — for example, in C++, interchangeably using a std::string or a const std::string I would expect to cause no performance difference (admittedly a compiler might be able to optimize code using the latter better by counting on the immutability, but I don’t know any real-world ones that do perform such theoretically possible optimizations;-).

    Having immutable strings may and does in fact allow very substantial optimizations in Java and Python. For example, if the strings get hashed, the hash can be cached, and will never have to be recomputed (since the string can’t change) — that’s especially important in Python, which uses hashed strings (for look-ups in sets and dictionaries) so lavishly and even “behind the scenes”. Fresh copies never need to be made “just in case” the previous one has changed in the meantime — references to a single copy can always be handed out systematically whenever that string is required. Python also copiously uses “interning” of (some) strings, potentially allowing constant-time comparisons and many other similarly fast operations — think of it as one more way, a more advanced one to be sure, to take advantage of strings’ immutability to cache more of the results of operations often performed on them.

    That’s not to say that a given compiler is going to take advantage of all possible optimizations, of course. For example, when a slice of a string is requested, there is no real need to make a new object and copy the data over — the new slice might refer to the old one with an offset (and an independently stored length), potentially a great optimization for big strings out of which many slices are taken. Python doesn’t do that because, unless particular care is taken in memory management, this might easily result in the “big” string being all kept in memory when only a small slice of it is actually needed — but it’s a tradeoff that a different implementation might definitely choose to perform (with that burden of extra memory management, to be sure — more complex, harder-to-debug compiler and runtime code for the hypothetical language in question).

    I’m just scratching the surface here — and many of these advantages would be hard to keep if otherwise interchangeable string types could exist in both mutable and immutable versions (which I suspect is why, to the best of my current knowledge at least, C++ compilers actually don’t bother with such optimizations, despite being generally very performance-conscious). But by offering only immutable strings as the primitive, fundamental data type (and thus implicitly accepting some disadvantage when you’d really need a mutable one;-), languages such as Java and Python can clearly gain all sorts of advantages — performance issues being only one group of them (Python’s choice to allow only immutable primitive types to be hashable, for example, is not a performance-centered design decision — it’s more about clarity and predictability of behavior for sets and dictionaries!-).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 542k
  • Answers 542k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Your code is totally on the right track if you… May 17, 2026 at 3:10 am
  • Editorial Team
    Editorial Team added an answer I was facing a similar intermittent problem on the app… May 17, 2026 at 3:10 am
  • Editorial Team
    Editorial Team added an answer jmac posted the original question on my behalf. I needed… May 17, 2026 at 3:10 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

In python, are strings mutable? The line someString[3] = a throws the error TypeError:
I've recent been reading about immutable strings Why can't strings be mutable in Java
It's the reverse of this question: Why can't strings be mutable in Java and
I'm trying to get my head around mutable vs immutable objects. Using mutable objects
I was just curious to know why structs, strings etc are immutable? What is
class Test { import scala.collection._ class Parent class Child extends Parent implicit val children
If I set a mutable string's value to a value from an array, and
I have following questions regarding strings in C++: 1>> which is a better option(considering
I read that non mutable data types can't be modified once created.(eg NSString or
I am wondering about the benefits of having the string-type immutable from the programmers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.