Skip to content

Instantly share code, notes, and snippets.

@hxzhouh
Created June 4, 2024 02:32
Show Gist options
  • Save hxzhouh/e8102d4c840e48501a8b84baddb7fe0b to your computer and use it in GitHub Desktop.
Save hxzhouh/e8102d4c840e48501a8b84baddb7fe0b to your computer and use it in GitHub Desktop.
unique string
package huizhou92
import (
"testing"
"unique"
)
var Token string
var tokenUnique unique.Handle[string]
//go:noinline
func BusinessString(t string) bool {
return t == Token
}
//go:noinline
func BusinessUnique(t unique.Handle[string]) bool {
return t.Value() == Token
}
func CreateString() string {
t := make([]byte, 10e6)
for i := 0; i < 10e6; i++ {
t[i] = 'a'
}
return string(t)
}
func init() {
Token = CreateString()
tokenUnique = unique.Make(Token)
}
func BenchmarkBusinessString(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
BusinessString(Token)
}
}
func BenchmarkBusinessUnique(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
BusinessUnique(tokenUnique)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment