1 package main 2 3 import ( 4 "fmt" 5 "strconv" 6 "time" 7 ) 8 9 const LOOP = 100000 10 11 var num int64 = 10 12 13 func main() { 14 startTime := time.Now() 15 for i := 0; i < LOOP; i++ { 16 fmt.Sprintf("%d", i) 17 } 18 fmt.Printf("fmt.Sprintf taken: %v\n", time.Since(startTime)) 19 20 startTime = time.Now() 21 for i := 0; i < LOOP; i++ { 22 strconv.Itoa(i) 23 } 24 fmt.Printf("strconv.FormatInt taken: %v\n", time.Since(startTime)) 25 }
得到的结果为
fmt.Sprintf taken: 23.0013ms
strconv.FormatInt taken: 4.0002ms
参考:http://deanthompson.github.io/tag/golang.html
时间: 2024-10-04 23:27:24