整形轉字符串經常會用到,本文討論一下 Golang 提供的這幾種方法?;?go1.10.1
fmt.Sprintf
fmt 包應該是最常見的了,從剛開始學習 Golang 就接觸到了,寫 ‘hello, world' 就得用它。它還支持格式化變量轉為字符串。
func Sprintf(format string, a ...interface{}) stringSprintf formats according to a format specifier and returns the resulting string.fmt.Sprintf("%d", a)
%d 代表十進制整數。
strconv.Itoa
func Itoa(i int) stringItoa is shorthand for FormatInt(int64(i), 10).strconv.Itoa(a)
strconv.FormatInt
func FormatInt(i int64, base int) stringFormatInt returns the string representation of i in the given base, for 2 <= base <= 36. The result uses the lower-case letters ‘a' to ‘z' for digit values >= 10.
參數 i 是要被轉換的整數, base 是進制,例如2進制,支持2到36進制。
strconv.Format(int64(a), 10)
Format 的實現
[0, 99)的兩位整數
對于小的(小于等于100)十進制正整數有加速優化算法:
if fastSmalls && 0 <= i && i < nSmalls && base == 10 { return small(int(i))}
加速的原理是提前算好100以內非負整數轉換后的字符串。
const smallsString = "00010203040506070809" + "10111213141516171819" + "20212223242526272829" + "30313233343536373839" + "40414243444546474849" + "50515253545556575859" + "60616263646566676869" + "70717273747576777879" + "80818283848586878889" + "90919293949596979899"
可以看出來,轉換后的結果是從1到99都有,而且每個結果只占兩位。當然個人數的情況還得特殊處理,個位數結果只有一位。
func small(i int) string { off := 0 if i < 10 { off = 1 } return smallsString[i*2+off : i*2+2]}
如果被轉換的數字是個位數,那么偏移量變成了1,默認情況是0。
只支持2到36進制的轉換。36進制是10個數字加26個小寫字母,超過這個范圍無法計算。
var a [64 + 1]byte
整形最大64位,加一位是因為有個符號。轉換計算時,要分10進制和非10進制的情況。
10進制轉換
10進制里,兩位兩位轉換,為什么這么干?兩位數字時100以內非負整數轉換可以用上面的特殊情況加速。很有意思。
us := uint(u)for us >= 100 { is := us % 100 * 2 us /= 100 i -= 2 a[i+1] = smallsString[is+1] a[i+0] = smallsString[is+0]}
2、4、8、16、32進制的轉換。
const digits = "0123456789abcdefghijklmnopqrstuvwxyz"var shifts = [len(digits) + 1]uint{ 1 << 1: 1, 1 << 2: 2, 1 << 3: 3, 1 << 4: 4, 1 << 5: 5,}if s := shifts[base]; s > 0 { // base is power of 2: use shifts and masks instead of / and % b := uint64(base) m := uint(base) - 1 // == 1<<s - 1 for u >= b { i-- a[i] = digits[uint(u)&m] u >>= s } // u < base i-- a[i] = digits[uint(u)]}
通過循環求余實現。進制的轉換也是這種方式。
for u >= b { i-- a[i] = uint(u)&m u >>= s}
上面的代碼實現了進制的轉換。而 digits[uint(u)&m] 實現了轉換后的結果再轉成字符。
常規情況
b := uint64(base)for u >= b { i-- q := u / b a[i] = digits[uint(u-q*b)] u = q}// u < basei--a[i] = digits[uint(u)]
依然是循環求余來實現。這段代碼更像是給人看的。和上面2的倍數的進制轉換的區別在于,上面的代碼把除法 / 換成了右移( >> ) s 位,把求余 % 換成了邏輯與 & 操作。
Sprintf 的實現
switch f := arg.(type) { case bool: p.fmtBool(f, verb) case float32: p.fmtFloat(float64(f), 32, verb) case float64: p.fmtFloat(f, 64, verb) case complex64: p.fmtComplex(complex128(f), 64, verb) case complex128: p.fmtComplex(f, 128, verb) case int: p.fmtInteger(uint64(f), signed, verb) ...}
判斷類型,如果是整數 int 類型,不需要反射,直接計算。支持的都是基礎類型,其它類型只能通過反射實現。
Sprintf 支持的進制只有10 %d 、16 x 、8 o 、2 b 這四種,其它的會包 fmt: unknown base; can't happen 異常。
switch base {case 10: for u >= 10 { i-- next := u / 10 buf[i] = byte('0' + u - next*10) u = next }case 16: for u >= 16 { i-- buf[i] = digits[u&0xF] u >>= 4 }case 8: for u >= 8 { i-- buf[i] = byte('0' + u&7) u >>= 3 }case 2: for u >= 2 { i-- buf[i] = byte('0' + u&1) u >>= 1 }default: panic("fmt: unknown base; can't happen")}
2、8、16進制和之前 FormatInt 差不多,而10進制的性能差一些,每次只能處理一位數字,而不像 FormatInt 一次處理兩位。
性能對比
var smallInt = 35var bigInt = 999999999999999func BenchmarkItoa(b *testing.B) { for i := 0; i < b.N; i++ { val := strconv.Itoa(smallInt) _ = val }}func BenchmarkItoaFormatInt(b *testing.B) { for i := 0; i < b.N; i++ { val := strconv.FormatInt(int64(smallInt), 10) _ = val }}func BenchmarkItoaSprintf(b *testing.B) { for i := 0; i < b.N; i++ { val := fmt.Sprintf("%d", smallInt) _ = val }}func BenchmarkItoaBase2Sprintf(b *testing.B) { for i := 0; i < b.N; i++ { val := fmt.Sprintf("%b", smallInt) _ = val }}func BenchmarkItoaBase2FormatInt(b *testing.B) { for i := 0; i < b.N; i++ { val := strconv.FormatInt(int64(smallInt), 2) _ = val }}func BenchmarkItoaBig(b *testing.B) { for i := 0; i < b.N; i++ { val := strconv.Itoa(bigInt) _ = val }}func BenchmarkItoaFormatIntBig(b *testing.B) { for i := 0; i < b.N; i++ { val := strconv.FormatInt(int64(bigInt), 10) _ = val }}func BenchmarkItoaSprintfBig(b *testing.B) { for i := 0; i < b.N; i++ { val := fmt.Sprintf("%d", bigInt) _ = val }}
壓測有三組對比,小于100的情況,大數字的情況,還有二進制的情況。
BenchmarkItoa-8 300000000 4.58 ns/op 0 B/op 0 allocs/opBenchmarkItoaFormatInt-8 500000000 3.07 ns/op 0 B/op 0 allocs/opBenchmarkItoaBase2Sprintf-8 20000000 86.4 ns/op 16 B/op 2 allocs/opBenchmarkItoaBase2FormatInt-8 50000000 30.2 ns/op 8 B/op 1 allocs/opBenchmarkItoaSprintf-8 20000000 83.5 ns/op 16 B/op 2 allocs/opBenchmarkItoaBig-8 30000000 44.6 ns/op 16 B/op 1 allocs/opBenchmarkItoaFormatIntBig-8 30000000 43.9 ns/op 16 B/op 1 allocs/opBenchmarkItoaSprintfBig-8 20000000 108 ns/op 24 B/op 2 allocs/op
本文涉及的代碼可以從 這里 下載。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答