Created
June 4, 2024 02:32
-
-
Save hxzhouh/e8102d4c840e48501a8b84baddb7fe0b to your computer and use it in GitHub Desktop.
unique string
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 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