Skip to content

Instantly share code, notes, and snippets.

@ken39arg
Created April 16, 2021 01:24
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 ken39arg/14cdf989da71717177482a1c6e5d50b1 to your computer and use it in GitHub Desktop.
Save ken39arg/14cdf989da71717177482a1c6e5d50b1 to your computer and use it in GitHub Desktop.
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