$ go test -bench . -benchmem
testing: warning: no tests to run
PASS
BenchmarkAtoi 50000000 28.6 ns/op 0 B/op 0 allocs/op
BenchmarkAtoiParseInt 50000000 27.0 ns/op 0 B/op 0 allocs/op
BenchmarkAtoiBig 20000000 57.2 ns/op 0 B/op 0 allocs/op
BenchmarkAtoiParseIntBig 30000000 54.6 ns/op 0 B/op 0 allocs/op
BenchmarkItoa 30000000 52.6 ns/op 2 B/op 1 allocs/op
BenchmarkItoaFormatInt 30000000 52.5 ns/op 2 B/op 1 allocs/op
BenchmarkItoaSprint 5000000 254 ns/op 16 B/op 2 allocs/op
BenchmarkItoaSprintf 5000000 299 ns/op 16 B/op 2 allocs/op
BenchmarkItoaBig 10000000 140 ns/op 16 B/op 1 allocs/op
BenchmarkItoaFormatIntBig 10000000 118 ns/op 16 B/op 1 allocs/op
BenchmarkItoaSprintBig 5000000 326 ns/op 24 B/op 2 allocs/op
BenchmarkItoaSprintfBig 5000000 363 ns/op 24 B/op 2 allocs/op
ok github.com/evalphobia/bench/ 26.708s-
-
Save shawel/3ca7b9369e421d220bd9eda3fa43b0ab to your computer and use it in GitHub Desktop.
golang benchmark: String and Int conversions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package bench | |
| import ( | |
| "strconv" | |
| "testing" | |
| ) | |
| var smallStr = "35" | |
| var bigStr = "999999999999999" | |
| func BenchmarkAtoi(b *testing.B) { | |
| for i := 0; i < b.N; i++ { | |
| val, _ := strconv.Atoi(smallStr) | |
| _ = val | |
| } | |
| } | |
| func BenchmarkAtoiParseInt(b *testing.B) { | |
| for i := 0; i < b.N; i++ { | |
| val, _ := strconv.ParseInt(smallStr, 0, 64) | |
| _ = val | |
| } | |
| } | |
| func BenchmarkAtoiBig(b *testing.B) { | |
| for i := 0; i < b.N; i++ { | |
| val, _ := strconv.Atoi(bigStr) | |
| _ = val | |
| } | |
| } | |
| func BenchmarkAtoiParseIntBig(b *testing.B) { | |
| for i := 0; i < b.N; i++ { | |
| val, _ := strconv.ParseInt(bigStr, 0, 64) | |
| _ = val | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package bench | |
| import ( | |
| "fmt" | |
| "strconv" | |
| "testing" | |
| ) | |
| var smallInt = 35 | |
| var bigInt = 999999999999999 | |
| func 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 BenchmarkItoaSprint(b *testing.B) { | |
| for i := 0; i < b.N; i++ { | |
| val := fmt.Sprint(smallInt) | |
| _ = val | |
| } | |
| } | |
| func BenchmarkItoaSprintf(b *testing.B) { | |
| for i := 0; i < b.N; i++ { | |
| val := fmt.Sprintf("%d", smallInt) | |
| _ = 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 BenchmarkItoaSprintBig(b *testing.B) { | |
| for i := 0; i < b.N; i++ { | |
| val := fmt.Sprint(bigInt) | |
| _ = val | |
| } | |
| } | |
| func BenchmarkItoaSprintfBig(b *testing.B) { | |
| for i := 0; i < b.N; i++ { | |
| val := fmt.Sprintf("%d", bigInt) | |
| _ = val | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment