Skip to content

Instantly share code, notes, and snippets.

@kamilsk
Created January 22, 2019 18:50
Show Gist options
  • Save kamilsk/af63aa5bb6178d4e4aeef091bdf32696 to your computer and use it in GitHub Desktop.
Save kamilsk/af63aa5bb6178d4e4aeef091bdf32696 to your computer and use it in GitHub Desktop.
Five way to build strings (strings builder are coming soon).
package main
import (
"fmt"
"reflect"
"testing"
"unsafe"
)
// goos: darwin
// goarch: amd64
// BenchmarkBuildStringByConcatenating-4 2000000 754 ns/op 800 B/op 10 allocs/op
// BenchmarkBuildStringByMasking-4 20000000 114 ns/op 0 B/op 0 allocs/op
// BenchmarkBuildStringBySprintf-4 500000 2132 ns/op 960 B/op 20 allocs/op
// BenchmarkBuildStringByUnsafe-4 20000000 76.1 ns/op 0 B/op 0 allocs/op
func BenchmarkBuildStringByConcatenating(b *testing.B) {
base := "https://guard.octolab.net/api/v1/license/"
ids := []string{
"072f5a3c-d612-4b91-af13-937e36e2aa93",
"3303ac29-8982-4220-a405-c5694b3f0baf",
"43fee265-a980-4bce-bb8b-8e780c67e047",
"9b8d8fdb-dd04-4632-beff-261b132231a0",
"8249a5cf-7bfb-4107-9bc0-f1a17dfa90e5",
"6b3b2eb5-478f-4746-97f8-3a2f2c5b8aef",
"d8de660b-739b-4bed-a961-df39822bbd3b",
"1bdb82bc-0077-404d-bf2a-dda69ded1ce4",
"bff662fd-95a6-4f60-92d9-a052bee6779c",
"838dc73f-983c-4cac-8690-252a4ea4bbaa",
}
var last string
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, id := range ids {
last = base + id
}
}
if last != "https://guard.octolab.net/api/v1/license/838dc73f-983c-4cac-8690-252a4ea4bbaa" {
b.Fail()
}
}
func BenchmarkBuildStringByMasking(b *testing.B) {
base := []rune("https://guard.octolab.net/api/v1/license/00000000-0000-0000-0000-000000000000")
ids := [][]rune{
[]rune("072f5a3c-d612-4b91-af13-937e36e2aa93"),
[]rune("3303ac29-8982-4220-a405-c5694b3f0baf"),
[]rune("43fee265-a980-4bce-bb8b-8e780c67e047"),
[]rune("9b8d8fdb-dd04-4632-beff-261b132231a0"),
[]rune("8249a5cf-7bfb-4107-9bc0-f1a17dfa90e5"),
[]rune("6b3b2eb5-478f-4746-97f8-3a2f2c5b8aef"),
[]rune("d8de660b-739b-4bed-a961-df39822bbd3b"),
[]rune("1bdb82bc-0077-404d-bf2a-dda69ded1ce4"),
[]rune("bff662fd-95a6-4f60-92d9-a052bee6779c"),
[]rune("838dc73f-983c-4cac-8690-252a4ea4bbaa"),
}
window := base[len(base)-len(ids[0]):]
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, id := range ids {
copy(window, id)
}
}
if string(base) != "https://guard.octolab.net/api/v1/license/838dc73f-983c-4cac-8690-252a4ea4bbaa" {
b.Fail()
}
}
func BenchmarkBuildStringBySprintf(b *testing.B) {
base := "https://guard.octolab.net/api/v1/license/%s"
ids := []string{
"072f5a3c-d612-4b91-af13-937e36e2aa93",
"3303ac29-8982-4220-a405-c5694b3f0baf",
"43fee265-a980-4bce-bb8b-8e780c67e047",
"9b8d8fdb-dd04-4632-beff-261b132231a0",
"8249a5cf-7bfb-4107-9bc0-f1a17dfa90e5",
"6b3b2eb5-478f-4746-97f8-3a2f2c5b8aef",
"d8de660b-739b-4bed-a961-df39822bbd3b",
"1bdb82bc-0077-404d-bf2a-dda69ded1ce4",
"bff662fd-95a6-4f60-92d9-a052bee6779c",
"838dc73f-983c-4cac-8690-252a4ea4bbaa",
}
var last string
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, id := range ids {
last = fmt.Sprintf(base, id)
}
}
if last != "https://guard.octolab.net/api/v1/license/838dc73f-983c-4cac-8690-252a4ea4bbaa" {
b.Fail()
}
}
func BenchmarkBuildStringByUnsafe(b *testing.B) {
base := "https://guard.octolab.net/api/v1/license/00000000-0000-0000-0000-000000000000"
hdr := (*reflect.StringHeader)(unsafe.Pointer(&base))
data := *(*[77]byte)(unsafe.Pointer(hdr.Data))
hdr.Data = uintptr(unsafe.Pointer(&data))
ids := [][]byte{
[]byte("072f5a3c-d612-4b91-af13-937e36e2aa93"),
[]byte("3303ac29-8982-4220-a405-c5694b3f0baf"),
[]byte("43fee265-a980-4bce-bb8b-8e780c67e047"),
[]byte("9b8d8fdb-dd04-4632-beff-261b132231a0"),
[]byte("8249a5cf-7bfb-4107-9bc0-f1a17dfa90e5"),
[]byte("6b3b2eb5-478f-4746-97f8-3a2f2c5b8aef"),
[]byte("d8de660b-739b-4bed-a961-df39822bbd3b"),
[]byte("1bdb82bc-0077-404d-bf2a-dda69ded1ce4"),
[]byte("bff662fd-95a6-4f60-92d9-a052bee6779c"),
[]byte("838dc73f-983c-4cac-8690-252a4ea4bbaa"),
}
window := data[len(data)-len(ids[0]):]
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, id := range ids {
copy(window, id)
}
}
if base != "https://guard.octolab.net/api/v1/license/838dc73f-983c-4cac-8690-252a4ea4bbaa" {
b.Fail()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment