19 lines
379 B
Go
19 lines
379 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
func main() {
|
|
const loopCount = 100000 // 10万次拼接
|
|
var result string
|
|
|
|
start := time.Now()
|
|
for i := 0; i < loopCount; i++ {
|
|
result += fmt.Sprintf("item_%d,", i) // 每次拼接生成新字符串,内存分配频繁
|
|
}
|
|
fmt.Printf("fmt循环拼接耗时:%v\n", time.Since(start))
|
|
_ = result // 避免未使用变量警告
|
|
}
|