Python and go string concatenation operations

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Some afternoon stroll Golang Chinese community, see a problem
A simple string performance test

With a shallow understanding of some languages, a little answer:

"You get the same results in Java.
In static languages such as go/java/c#, the value of string is immutable. Each "+" operation on the string requires a copy of the original string again.
So these languages involve the manipulation of long strings, not the use of "+", but something like joins or slices. "

But then it came to mind that a string similar in Python is immutable. So why is my answer wrong?
Since the string type is immutable, a new object is bound to be generated in Python. But why is it so fast?

Google searched a circle can not find the available information, to see the source code and do not know where the entrance. So I can only ask StackOverflow.
What's the different from string ' s "+" operation between Golang and Python?

So far StackOverflow has not answered, but enthusiastic netizens have given a lot of important information on this issue.

Along with the information they give, plus my own explorations, make the following summary:

  1. In Python, the ' + = ' operation of the string is optimized

    In Python, the string is clearly an immutable type, but the interpreter (referred to as CPython, hereinafter) encounters an expression like str_x + = str_y or str_x = str_x + str_y, or it will trickery change the value of string, but is strictly required: The left value is not referenced by another expression and satisfies the two-dollar operation.

    s = ''for i in range(100000):    s += 'test' # or s = s + 'test'#Out: 0:00:00.019121

    However , if you do not satisfy a two-dollar operation or are referenced by another expression, it is like this:

    str_x = str_x + str_y + str_z

    Or

    str_x += str_x + str_y + str_z

    Or

    str_x = str_x + str_ystr_a += str_x#(↑循环10w次飙完了我的内存)

    The interpreter will not be optimized.

  2. It is also recommended that you do not use the ' + = ' operation and you should use the Join function.

    While this might make the code look more concise, it is easy to err when you are careful. When the amount of data is very large, wait to cry, and it is possible that because of the creation of new objects, and other expressions can not be purged by the GC, all of a sudden to complete your memory, the above is an example.

  3. Go Optimization method

    The string type in a language such as Go or Java is strictly invariant and does not have a python-like trickery optimization. Here are two correct poses for the go string concatenation from StackOverflow:

      The
    1. uses bytes. Buffer type:
        var buffer bytes. Bufferfor N: = 0; n < 100000; n++ {buffer.  WriteString ("Test")}  
    2. using the copy function
      bs: = make ([]byte, 100000) BL: = 0for N: = 0; n < 100000; n++ {bl + = copy (BS[BL:], "test")}  

    Method Two is the efficiency is about 15 times times the method one, and method one is the "+ =" operation of 10w times.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.