Created
April 16, 2021 01:24
-
-
Save ken39arg/14cdf989da71717177482a1c6e5d50b1 to your computer and use it in GitHub Desktop.
This file contains 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 main_test | |
import ( | |
"fmt" | |
"strconv" | |
"testing" | |
) | |
var ( | |
prefix = "abcdefghi:" | |
id = 123456 | |
) | |
func BenchmarkSprintPlus(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
_ = prefix + fmt.Sprintf("%d", id) | |
} | |
} | |
func BenchmarkSprintOnly(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
_ = fmt.Sprintf("%s%d", prefix, id) | |
} | |
} | |
func BenchmarkItoa(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
_ = prefix + strconv.Itoa(id) | |
} | |
} | |
func BenchmarkFormatInt(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
_ = prefix + strconv.FormatInt(int64(id), 10) | |
} | |
} | |
// goos: darwin | |
// goarch: amd64 | |
// BenchmarkSprintPlus-4 8625607 123 ns/op 16 B/op 2 allocs/op | |
// BenchmarkSprintOnly-4 6326647 168 ns/op 40 B/op 3 allocs/op | |
// BenchmarkItoa-4 23701102 49.4 ns/op 8 B/op 1 allocs/op | |
// BenchmarkFormatInt-4 23848172 48.8 ns/op 8 B/op 1 allocs/op |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment