-
-
Save mgartner/28a9d52e54e91691d0a7617164aa3157 to your computer and use it in GitHub Desktop.
Concat benchmark
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 concat | |
import ( | |
"fmt" | |
"strconv" | |
"strings" | |
"testing" | |
) | |
func concatFmt(a, b uint64) string { | |
return fmt.Sprintf("%d@%d", a, b) | |
} | |
func concatPlus(a, b uint64) string { | |
const base = 10 | |
return strconv.FormatUint(a, base) + "@" + strconv.FormatUint(b, base) | |
} | |
func concatSB(a, b uint64) string { | |
const base = 10 | |
var sb strings.Builder | |
sb.WriteString(strconv.FormatUint(a, base)) | |
sb.WriteByte('@') | |
sb.WriteString(strconv.FormatUint(b, base)) | |
return sb.String() | |
} | |
func BenchmarkConcat(b *testing.B) { | |
type testCase struct { | |
name string | |
a, b uint64 | |
} | |
tcs := []testCase{ | |
{"zeros", 0, 0}, | |
{"small-big", 123, 9120497109247123}, | |
{"big-small", 9120497109247123, 123}, | |
{"big-big", 9120497109247123, 9120497109247123}, | |
} | |
b.Run("concatFmt", func (b *testing.B) { | |
for _, tc := range tcs { | |
b.Run(tc.name, func (b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
_ = concatFmt(tc.a, tc.b) | |
} | |
}) | |
} | |
}) | |
b.Run("concatPlus", func (b *testing.B) { | |
for _, tc := range tcs { | |
b.Run(tc.name, func (b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
_ = concatPlus(tc.a, tc.b) | |
} | |
}) | |
} | |
}) | |
b.Run("concatSB", func (b *testing.B) { | |
for _, tc := range tcs { | |
b.Run(tc.name, func (b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
_ = concatSB(tc.a, tc.b) | |
} | |
}) | |
} | |
}) | |
} |
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
➜ go test -bench . -benchmem | |
goos: darwin | |
goarch: arm64 | |
pkg: goplay/concat | |
cpu: Apple M1 Pro | |
BenchmarkConcat/concatFmt/zeros-10 19883263 60.31 ns/op 3 B/op 1 allocs/op | |
BenchmarkConcat/concatFmt/small-big-10 13212832 90.80 ns/op 32 B/op 2 allocs/op | |
BenchmarkConcat/concatFmt/big-small-10 13258746 90.19 ns/op 32 B/op 2 allocs/op | |
BenchmarkConcat/concatFmt/big-big-10 11662374 102.7 ns/op 64 B/op 3 allocs/op | |
BenchmarkConcat/concatPlus/zeros-10 46471173 25.29 ns/op 3 B/op 1 allocs/op | |
BenchmarkConcat/concatPlus/small-big-10 17164700 68.82 ns/op 43 B/op 3 allocs/op | |
BenchmarkConcat/concatPlus/big-small-10 17233651 68.93 ns/op 43 B/op 3 allocs/op | |
BenchmarkConcat/concatPlus/big-big-10 15554778 76.36 ns/op 80 B/op 3 allocs/op | |
BenchmarkConcat/concatSB/zeros-10 52523403 21.87 ns/op 8 B/op 1 allocs/op | |
BenchmarkConcat/concatSB/small-big-10 15188575 79.08 ns/op 56 B/op 4 allocs/op | |
BenchmarkConcat/concatSB/big-small-10 14948676 80.04 ns/op 67 B/op 4 allocs/op | |
BenchmarkConcat/concatSB/big-big-10 11224966 107.1 ns/op 144 B/op 5 allocs/op | |
PASS | |
ok goplay/concat 16.240s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment