Notes Performance comparison of the string assembling mode of Go language

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

There are many ways to assemble strings in the go language, so the question is, which is the best performance?

The following code, respectively, compares the FMT. Sprintf,string +,strings. Join,bytes. Buffer, the method is to loop several times the total time of comparison.

The results of the operation under the Ubuntu 14.04 under VMware Show:

    • Fmt. Sprintf and strings. Join speed is quite
    • String + one times faster than the above
    • bytes. And the buffer is about 400-500 times faster than the last person.
    • If a bytes is temporarily declared within a loop. Buffer to use, will be 50% slower than persistent, but still very fast

The test code is as follows:

Package Mainimport ("bytes"    "FMT"    "Strings"    " Time") func benchmarkstringfunction (nint, indexint) (d time. Duration) {V:="ni shuo wo shi bu shi tai wu liao le a?"    varSstring    varbuf bytes. Buffer T0:=Time . Now () forI: =0; I < n; i++ {        SwitchIndex { Case 0://FMT. Sprintfs = fmt. Sprintf ("%s[%s]", S, v) Case 1://string +s = s +"["+ V +"]"         Case 2://strings. Joins = strings. Join ([]string{s,"["V"]"},"")         Case 3://Temporary bytes. BufferB: =bytes. buffer{} b.writestring ("[") b.writestring (v) b.writestring ("]") s=b.string () Case 4://Stable bytes. BufferBuf. WriteString ("[") buf. WriteString (v) buf. WriteString ("]")        }        ifi = = N1 {            ifindex = =4{//For stable bytes. Buffers =buf. String ()} FMT. Println (Len (s))//consume s to avoid compiler optimization}} T1:=Time . Now () d=t1. Sub (t0) fmt. Printf ("Time of the (%d) =%v\n", index, D)returnD}func Main () {k:=5D:= [5]time. duration{} forI: =0; I < K; i++{D[i]= Benchmarkstringfunction (10000, I)}  forI: =0; I < K1; i++{fmt. Printf ("The on-the-%6.1f times of the- %d\n", I, float32 (D[i])/float32 (d[k-1]), K-1)    }}

One of the results was as follows:

etworker@ubuntu:~/work/go/test$ Go run testbenchmark.go410000Time of the -0)=1. 199641573s410000Time of the -1)=568. 716669ms410000Time of the -2)=1. 197077483s ATime of the -3)=2. 277063ms410000Time of the -4)=1. 398864msway0  is  857.6Times of the4 the1  is  406.6Times of the4 the2  is  855.7Times of the4 the3  is    1.6Times of the4

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.