Skip to content

Instantly share code, notes, and snippets.

@erjiaqing
Last active March 19, 2019 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erjiaqing/53dd66c828ac1606cfef0c7f7d3e16fc to your computer and use it in GitHub Desktop.
Save erjiaqing/53dd66c828ac1606cfef0c7f7d3e16fc to your computer and use it in GitHub Desktop.
Find fastest method to get length in golang
package main
import (
"testing"
)
var smallInt = []int{1, 32, 102, 47, 2222, 986, 0, 2}
var bigInt = []int{
1234567890, 123456789, 123456789012, 123, 1,
12, 12345667801234, 1234567890123, 1234, 12345678901,
123456789012345, 1234567890123456, 12345678, 1234567, 12345,
123456,
}
// func BenchmarkGetLen(b *testing.B) {
// for i := 0; i < b.N; i++ {
// val := len(strconv.Itoa(smallInt[i&7]))
// _ = val
// }
// }
// func BenchmarkGetLenFormatInt(b *testing.B) {
// for i := 0; i < b.N; i++ {
// val := len(strconv.FormatInt(int64(smallInt[i&7]), 10))
// _ = val
// }
// }
// func BenchmarkGetLenSprint(b *testing.B) {
// for i := 0; i < b.N; i++ {
// val := len(fmt.Sprint(smallInt[i&7]))
// _ = val
// }
// }
// func BenchmarkGetLenSprintf(b *testing.B) {
// for i := 0; i < b.N; i++ {
// val := len(fmt.Sprintf("%d", smallInt[i&7]))
// _ = val
// }
// }
// func BenchmarkGetLenBig(b *testing.B) {
// for i := 0; i < b.N; i++ {
// val := len(strconv.Itoa(bigInt[i&15]))
// _ = val
// }
// }
// func BenchmarkGetLenFormatIntBig(b *testing.B) {
// for i := 0; i < b.N; i++ {
// val := len(strconv.FormatInt(int64(bigInt[i&15]), 10))
// _ = val
// }
// }
// func BenchmarkGetLenSprintBig(b *testing.B) {
// for i := 0; i < b.N; i++ {
// val := len(fmt.Sprint(bigInt[i&15]))
// _ = val
// }
// }
// func BenchmarkGetLenSprintfBig(b *testing.B) {
// for i := 0; i < b.N; i++ {
// val := len(fmt.Sprintf("%d", bigInt[i&15]))
// _ = val
// }
// }
func BenchmarkGetLenDivide(b *testing.B) {
for i := 0; i < b.N; i++ {
ret := smallInt[i&7]
val := 1
for ret > 9 {
ret /= 10
val++
}
_ = val
}
}
func BenchmarkGetLenDivideBig(b *testing.B) {
for i := 0; i < b.N; i++ {
ret := bigInt[i&15]
val := 1
for ret > 9 {
ret /= 10
val++
}
_ = val
}
}
var res = [20]int{
0, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000,
100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 10000000000000000, 100000000000000000, 1000000000000000000, (1 << 63) - 1,
}
func countLength(num int) int {
val := 0
if num >= 10000 {
val = 4
}
if num >= 1000000000 {
val = 9
}
for ; val < 19; val++ {
if num >= res[val] {
return val + 1
}
}
return 1
}
func countLengthNoPrep(num int) int {
for val := 0; val < 19; val++ {
if num >= res[val] {
return val + 1
}
}
return 1
}
func BenchmarkGetLenLookUpTable(b *testing.B) {
for i := 0; i < b.N; i++ {
val := countLength(smallInt[i&7])
_ = val
}
}
func BenchmarkGetLenLookUpTable2(b *testing.B) {
for i := 0; i < b.N; i++ {
val := countLengthNoPrep(smallInt[i&7])
_ = val
}
}
func BenchmarkGetLenLookUpTableBig(b *testing.B) {
for i := 0; i < b.N; i++ {
val := countLength(bigInt[i&15])
_ = val
}
}
func BenchmarkGetLenLookUpTableBig2(b *testing.B) {
for i := 0; i < b.N; i++ {
val := countLengthNoPrep(bigInt[i&15])
_ = val
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment