...
from https://blog.csdn.net/m0_37422289/article/details/103362740
append 的最快
package main import ( "bytes" "fmt" "strings" "time" ) type Enet_smp_domain_map struct { // gorm.Model Id uint `gorm:"primary_key;auto_increment;not null"` Domain_name string Domain_ip string Domain_count uint CreatedAt time.Time } var loop = 100000 func main() { // db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{}) // if err != nil { // panic("failed to connect database") // } // db.AutoMigrate(&Enet_smp_domain_map{}) // db.Create(&Enet_smp_domain_map{Domain_name: "app.baidu.com", Domain_ip: "1.1.1.1", Domain_count: 3305}) // db.Create(&Enet_smp_domain_map{Domain_name: "app.baidu.com", Domain_ip: "1.1.10.1", Domain_count: 3305}) // db.Create(&Enet_smp_domain_map{Domain_name: "app.baidu.com", Domain_ip: "1.1.11.1", Domain_count: 3305}) var s string s1 := "hello" s2 := "world" var start time.Time //加号+连接 start = time.Now() for i := 0; i < loop; i++ { s1 += s2 } fmt.Println("+连接方法:", time.Since(start)) //append连接 s1 = "hello" s2 = "world" start = time.Now() for i := 0; i < loop; i++ { s = string(append([]byte(s1), s2...)) } fmt.Println("append方法:", time.Since(start)) //Join方法连接 v := []string{"hello", "world"} start = time.Now() for i := 0; i < loop; i++ { s = strings.Join(v, "") } fmt.Println("strings.Join方法:", time.Since(start)) //bytes.writestring方法 start = time.Now() for i := 0; i < loop; i++ { var buf bytes.Buffer buf.WriteString("hello") buf.WriteString("world") buf.String() } fmt.Println("bytes.writestring方法:", time.Since(start)) //fmt方法连接 start = time.Now() for i := 0; i < loop; i++ { s = fmt.Sprintf("%s%s", "hello", "world") } fmt.Println("fmt方法:", time.Since(start)) fmt.Println(s) }
标签:Println,Domain,fmt,db,start,拼接,time,字符串,效率 From: https://www.cnblogs.com/eiguleo/p/17430934.html