package biginthelper import ( "fmt" "math/rand" "strconv" "strings" "testing" ) func Test_ParseBigInt(t *testing.T) { fmt.Println(ParseBigInt(`231513`)) fmt.Println(ParseBigInt(`654wdf16`)) fmt.Println(ParseBigInt(`5455_1655`)) fmt.Println(ParseBigInt(``)) fmt.Println(ParseBigInt(`af`)) } func Test_IntToHex(t *testing.T) { fmt.Println(strings.TrimLeft(`01615`, "0")) fmt.Println(strings.TrimLeft(`1615`, "0")) fmt.Println(strings.TrimLeft(`0x1615`, "0")) } // i := int64(32) // s := strconv.FormatInt(i, 16) // println(s) // 对比strconv.FormatInt(i, 16)和fmt.Sprintf("0x%x", i)的性能消耗 // go test -bench=_QE_ -benchmem // -benchtime 默认为1秒 -benchmem 获得内存分配的统计数据 func Benchmark_QE_strconv(b *testing.B) { for i := 0; i < b.N; i++ { strconv.FormatInt(getInt64(), 16) } } func Benchmark_QE_fmt(b *testing.B) { for i := 0; i < b.N; i++ { fmt.Sprintf("0x%x", getInt64()) } } func getInt64() int64 { return int64(rand.Intn(10000)) } // 结果 // Benchmark_QE_strconv-6 47142570 24.29 ns/op 5 B/op 1 allocs/op // Benchmark_QE_fmt-6 14787649 82.41 ns/op 8 B/op 1 allocs/op // 改为随机数后 // Benchmark_QE_strconv-6 27890760 42.48 ns/op 3 B/op 0 allocs/op // Benchmark_QE_fmt-6 10595380 108.6 ns/op 15 B/op 1 allocs/op // 结论 尽量使用 strconv.FormatInt(i, 16) 进行16进制的转换