首次提交:初始化项目代码
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
const loopCount = 10000000 // 1000万次循环,模拟高频场景
|
||||
|
||||
// 一、整数→字符串
|
||||
// 1. fmt.Sprintf整数转字符串
|
||||
fmtStart := time.Now()
|
||||
for i := 0; i < loopCount; i++ {
|
||||
_ = fmt.Sprintf("%d", i)
|
||||
}
|
||||
fmt.Printf("整数→字符串 fmt.Sprintf耗时:%v\n", time.Since(fmtStart))
|
||||
|
||||
// 2. strconv.Itoa整数转字符串
|
||||
strconvStart := time.Now()
|
||||
for i := 0; i < loopCount; i++ {
|
||||
_ = strconv.Itoa(i)
|
||||
}
|
||||
fmt.Printf("整数→字符串 strconv.Itoa耗时:%v\n", time.Since(strconvStart))
|
||||
|
||||
// 二、浮点数→字符串
|
||||
// 1. fmt.Sprintf浮点数转字符串
|
||||
fmtStart2 := time.Now()
|
||||
for i := 0; i < loopCount; i++ {
|
||||
_ = fmt.Sprintf("%.2f", 3.14)
|
||||
}
|
||||
fmt.Printf("浮点数→字符串 fmt.Sprintf耗时:%v\n", time.Since(fmtStart2))
|
||||
|
||||
// 2. strconv.FormatFloat浮点数转字符串
|
||||
strconvStart2 := time.Now()
|
||||
for i := 0; i < loopCount; i++ {
|
||||
_ = strconv.FormatFloat(3.14, 'f', 2, 64)
|
||||
}
|
||||
fmt.Printf("浮点数→字符串 strconv.FormatFloat耗时:%v\n", time.Since(strconvStart2))
|
||||
|
||||
// 三、布尔值→字符串
|
||||
// 1. fmt.Sprintf布尔值转字符串
|
||||
strconvStart3 := time.Now()
|
||||
for i := 0; i < loopCount; i++ {
|
||||
_ = fmt.Sprintf("%t", true)
|
||||
}
|
||||
fmt.Printf("布尔值→字符串 fmt.Sprintf耗时:%v\n", time.Since(strconvStart3))
|
||||
|
||||
// 2. fmt.FormatBool布尔值转字符串
|
||||
fmtStart3 := time.Now()
|
||||
for i := 0; i < loopCount; i++ {
|
||||
_ = strconv.FormatBool(true)
|
||||
}
|
||||
fmt.Printf("布尔值→字符串 strconv.FormatBool耗时:%v\n", time.Since(fmtStart3))
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user