Benchmarks test for Go character stitching

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

in the use of Go to do a SQL generation tool, the performance of his string splicing is curious, in Google search for half a day, as if serious foreigners do not have this result, so get one of them!

First come to the conclusion

Times
1th time 2nd 3rd times 4th 5th
2 parameters/1 million times
Fmt. Sprintf 0.5550317 0.5800332 0.5800332 0.5090291 0.5160295
bytes. Newbuffer 0.9450541 1.0010573 0.9430539 0.9500544 0.9600549
+ 0.1460083 0.1700097 0.1470085 0.1450083 0.1560089
Strings. Join 0.279016 0.3030173 0.2820161 0.2760158 0.3080176
4 parameters/1 million times
Fmt. Sprintf 1.0260587 0.944054 1.0270587 0.944054 0.9530545
bytes. Newbuffer 1.4470828 1.3770788 1.3970799 1.5100864 1.4890852
+ 0.2920167 0.2580147 0.2630151 0.2880164 0.2860163
Strings. Join 0.4670267 0.4560261 0.4700269 0.4620265 0.4630265
6 parameters/1 million times
Fmt. Sprintf 1.4090806 1.3880794 1.4110808 1.4290817 1.3900795
bytes. Newbuffer 1.5800904 1.5240871 1.5440883 1.5990915 1.57209
+ 0.4000229 0.4030231 0.3940225 0.4110235 0.4080233
Strings. Join 0.5630322 0.6270358 0.6280359 0.5840334 0.6340363

Oh, yes, the string + is the fastest, of course, here is just a simple test results, the situation is to choose the most consistent with the use of the scene method.

If there is a new result, it will be updated to this side: Http://git.oschina.net/janpoem/go-benchmarks

Test code

Package Mainimport ("Git.oschina.net/janpoem/go-agi") Import ("FMT" "Math/rand" "StrConv" "bytes" "Stri NGS ") Func Randstr () (string) {return StrConv. Formatint (Rand. Int63 (),}var sp_str1 = Randstr () var sp_str2 = Randstr () var sp_str3 = Randstr () var sp_str4 = Randstr () var SP_STR5 = Ran Dstr () var sp_str6 = Randstr () func Test_2_args () {max: = 1000000 dur1: = Agi. Timestest (Max, func () {fmt. Sprintf ("Select%s from%s", SP_STR1, SP_STR2)}) Dur2: = Agi. Timestest (Max, func () {bf: = bytes. Newbufferstring ("select") bf. WriteString ("") bf. WriteString (SP_STR1) bf. WriteString ("") bf. WriteString ("from") bf. WriteString (SP_STR2)}) Dur3: = Agi.    Timestest (Max, func () {bf: = "Select" + Sp_str1 + "from" + sp_str2 If Len (BF) > 0 {}}) DUR4: = Agi. Timestest (Max, func () {bf: = strings.      Join ([]string {"Select", Sp_str1, "from", sp_str2}, "") If Len (BF) > 0 {  }}) Fmt. Println (Dur1. Seconds ()) fmt. Println (DUR2. Seconds ()) fmt. Println (DUR3. Seconds ()) fmt. Println (DUR4. Seconds ())}func Test_4_args () {max: = 1000000 dur1: = Agi. Timestest (Max, func () {fmt. Sprintf ("Select%s from%s WHERE%s is ORDER by%s", Sp_str1, Sp_str2, SP_STR3, SP_STR4)}) Dur2: = Agi. Timestest (Max, func () {bf: = bytes. Newbufferstring ("select") bf. WriteString (SP_STR1) bf. WriteString ("") bf. WriteString ("from") bf. WriteString (SP_STR2) bf. WriteString ("") bf. WriteString ("WHERE") bf. WriteString (SP_STR3) bf. WriteString ("") bf. WriteString ("ORDER by") Bf. WriteString (SP_STR4)}) Dur3: = Agi. Timestest (Max, func () {bf: = "Select" + Sp_str1 + "from" + sp_str2 + "WHERE" + Sp_str3 + "ORDER by" + Sp_st R4 If Len (BF) > 0 {}}) Dur4: = Agi. Timestest (Max, func () {bf: = strings. Join ([]string {"Select", Sp_str1, "from", Sp_str2, "WHERE", Sp_str3, "ORDER by", SP_STR4}, "") If Len (BF) > 0 {}}) fmt. Println (Dur1. Seconds ()) fmt. Println (DUR2. Seconds ()) fmt. Println (DUR3. Seconds ()) fmt. Println (DUR4. Seconds ())}func Test_6_args () {max: = 1000000 dur1: = Agi. Timestest (Max, func () {fmt. Sprintf ("Select%s from%s WHERE%s is the ORDER by%s GROUP by%s LIMIT%s", Sp_str1, Sp_str2, SP_STR3, SP_STR4, SP_STR5, Sp_str 6)}) DUR2: = Agi. Timestest (Max, func () {bf: = bytes. Newbufferstring ("select") bf. WriteString (SP_STR1) bf. WriteString ("") bf. WriteString ("from") bf. WriteString (SP_STR2) bf. WriteString ("") bf. WriteString ("WHERE") bf. WriteString (SP_STR3) bf. WriteString ("") bf. WriteString ("ORDER by") Bf. WriteString (SP_STR4) bf. WriteString ("") bf. WriteString ("GROUP by") Bf. WriteString (SP_STR5) bf. WriteString ("") bf. WriteString ("LIMIT") bf. WriteString (sp_sTR6)}) Dur3: = Agi. Timestest (Max, func () {bf: = "Select" + Sp_str1 + "from" + sp_str2 + "WHERE" + SP_STR3 + "O Rder by "+ SP_STR4 +" GROUP by "+ SP_STR5 +" LIMIT "+ SP_STR6 If Len (BF) > 0 {}} ) Dur4: = Agi. Timestest (Max, func () {bf: = strings.            Join ([]string {"Select", Sp_str1, "from", Sp_str2, "WHERE", Sp_str3, "ORDER by", SP_STR4, "GROUP by", SP_STR5, "LIMIT", SP_STR6,}, "") If Len (BF) > 0 {}}) fmt. Println (Dur1. Seconds ()) fmt. Println (DUR2. Seconds ()) fmt. Println (DUR3. Seconds ()) fmt. Println (DUR4. Seconds ())}func main () {//Test_2_args ()//Test_4_args ()//Test_6_args ()}


The specific code is here: Http://git.oschina.net/janpoem/go-benchmarks/blob/master/str_concat.go

Timestest, the call is this: Benchmark.go, his function is to execute the specified number of anonymous functions, depending on how many times you pass in. At first, there is some concern about the performance of anonymous functions, but compared to the execution time of bare code and anonymous function, the difference is not much, it is ignored.

This test is just a test of the execution time and does not include CPU and memory condition monitoring.

Er, the extra said, @ Sweet Potato Blog markdown Editor is not too wonderful, table, code, what format is not supported, too difficult to use.


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.