Performance comparison of Golang string assembling method

Source: Internet
Author: User
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 operating under Ubuntu 14.04 under VMware are as follows, for reference only:

    • 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 (n int, index int) (d time.    Duration) {V: = "ni shuo wo shi bu shi tai wu liao le a?" var s string var buf bytes. Buffer T0: = time. Now () for I: = 0; I < n; i++ {Switch Index {case 0://FMT. Sprintf s = fmt. Sprintf ("%s[%s]", S, v) Case 1://string + S = s + "[" + V + "]" Case 2://Strings. Join s = strings. Join ([]string{s, "[", V, "]"}, "") Case 3://Temporary bytes. Buffer B: = bytes.         buffer{} b.writestring ("[") b.writestring (v) b.writestring ("]") s = b.string () Case 4://Stable bytes. Buffer buf. WriteString ("[") buf. WriteString (v) buf. WriteString ("]")} if I = = n-1 {if index = = 4 {//for stable bytes. Buffer s = buf. String ()} FMT. Println (Len (s))//conSume s to avoid compiler optimization}} T1: = time. Now () d = t1. Sub (t0) fmt. Printf ("Time of the-=%v\n", index, D) return D}func main () {k: = 5 D: = [5]time. duration{} for I: = 0; I < K; i++ {D[i] = Benchmarkstringfunction (10000, i)} for I: = 0; i < k-1; i++ {fmt. Printf ("by-%6.1f times of the-%d\n", I, float32 (D[i])/float32 (d[k-1]), k-1)}}

One of the results was as follows:

410000time of way(0)=1.199641573s410000time of way(1)=568.716669ms410000time of way(2)=1.197077483s41time of way(3)=2.277063ms410000time of way(4)=1.398864msway 0 is  857.6 times of way 4way 1 is  406.6 times of way 4way 2 is  855.7 times of way 4way 3 is    1.6 times of way 4

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.