首页 > 代码库 > fmt.Sprintf strconv.Itoa 效率实验

fmt.Sprintf strconv.Itoa 效率实验

 1 package main 2  3 import ( 4     "fmt" 5     "strconv" 6     "time" 7 ) 8  9 const LOOP = 10000010 11 var num int64 = 1012 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

fmt.Sprintf strconv.Itoa 效率实验