Last active
December 26, 2022 03:11
-
-
Save x893675/88804dc14464af63e4928627b9b01292 to your computer and use it in GitHub Desktop.
create random 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 main | |
import ( | |
"fmt" | |
"math/rand" | |
"strings" | |
"time" | |
"unsafe" | |
) | |
// Implementations | |
func init() { | |
rand.Seed(time.Now().UnixNano()) | |
} | |
func main() { | |
fmt.Println(RandStringBytesMaskImprSrcUnsafe(16)) | |
} | |
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") | |
func RandStringRunes(n int) string { | |
b := make([]rune, n) | |
for i := range b { | |
b[i] = letterRunes[rand.Intn(len(letterRunes))] | |
} | |
return string(b) | |
} | |
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
const ( | |
letterIdxBits = 6 // 6 bits to represent a letter index | |
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits | |
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits | |
) | |
func RandStringBytes(n int) string { | |
b := make([]byte, n) | |
for i := range b { | |
b[i] = letterBytes[rand.Intn(len(letterBytes))] | |
} | |
return string(b) | |
} | |
func RandStringBytesRmndr(n int) string { | |
b := make([]byte, n) | |
for i := range b { | |
b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))] | |
} | |
return string(b) | |
} | |
func RandStringBytesMask(n int) string { | |
b := make([]byte, n) | |
for i := 0; i < n; { | |
if idx := int(rand.Int63() & letterIdxMask); idx < len(letterBytes) { | |
b[i] = letterBytes[idx] | |
i++ | |
} | |
} | |
return string(b) | |
} | |
func RandStringBytesMaskImpr(n int) string { | |
b := make([]byte, n) | |
// A rand.Int63() generates 63 random bits, enough for letterIdxMax letters! | |
for i, cache, remain := n-1, rand.Int63(), letterIdxMax; i >= 0; { | |
if remain == 0 { | |
cache, remain = rand.Int63(), letterIdxMax | |
} | |
if idx := int(cache & letterIdxMask); idx < len(letterBytes) { | |
b[i] = letterBytes[idx] | |
i-- | |
} | |
cache >>= letterIdxBits | |
remain-- | |
} | |
return string(b) | |
} | |
var src = rand.NewSource(time.Now().UnixNano()) | |
func RandStringBytesMaskImprSrc(n int) string { | |
b := make([]byte, n) | |
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters! | |
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; { | |
if remain == 0 { | |
cache, remain = src.Int63(), letterIdxMax | |
} | |
if idx := int(cache & letterIdxMask); idx < len(letterBytes) { | |
b[i] = letterBytes[idx] | |
i-- | |
} | |
cache >>= letterIdxBits | |
remain-- | |
} | |
return string(b) | |
} | |
func RandStringBytesMaskImprSrcSB(n int) string { | |
sb := strings.Builder{} | |
sb.Grow(n) | |
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters! | |
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; { | |
if remain == 0 { | |
cache, remain = src.Int63(), letterIdxMax | |
} | |
if idx := int(cache & letterIdxMask); idx < len(letterBytes) { | |
sb.WriteByte(letterBytes[idx]) | |
i-- | |
} | |
cache >>= letterIdxBits | |
remain-- | |
} | |
return sb.String() | |
} | |
func RandStringBytesMaskImprSrcUnsafe(n int) string { | |
b := make([]byte, n) | |
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters! | |
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; { | |
if remain == 0 { | |
cache, remain = src.Int63(), letterIdxMax | |
} | |
if idx := int(cache & letterIdxMask); idx < len(letterBytes) { | |
b[i] = letterBytes[idx] | |
i-- | |
} | |
cache >>= letterIdxBits | |
remain-- | |
} | |
return *(*string)(unsafe.Pointer(&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
package main | |
import "testing" | |
// Benchmark functions | |
const n = 16 | |
func BenchmarkRunes(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
RandStringRunes(n) | |
} | |
} | |
func BenchmarkBytes(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
RandStringBytes(n) | |
} | |
} | |
func BenchmarkBytesRmndr(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
RandStringBytesRmndr(n) | |
} | |
} | |
func BenchmarkBytesMask(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
RandStringBytesMask(n) | |
} | |
} | |
func BenchmarkBytesMaskImpr(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
RandStringBytesMaskImpr(n) | |
} | |
} | |
func BenchmarkBytesMaskImprSrc(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
RandStringBytesMaskImprSrc(n) | |
} | |
} | |
func BenchmarkBytesMaskImprSrcSB(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
RandStringBytesMaskImprSrcSB(n) | |
} | |
} | |
func BenchmarkBytesMaskImprSrcUnsafe(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
RandStringBytesMaskImprSrcUnsafe(n) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
go test -bench=. ./main_test.go ./main.go