lo
lo copied to clipboard
refactor:refactor RandomString function
reference resources:How to generate a random string of a fixed length in Go?
func BenchmarkApproach1(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = RandomString(1<<10, lo.AlphanumericCharset)
}
}
func BenchmarkApproach2(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = lo.RandomString(1<<10, lo.AlphanumericCharset)
}
}
$ go test -benchmem -benchtime=5s -bench .
goos: windows
goarch: amd64
pkg: mytttttt
cpu: Intel(R) Core(TM) i5-10400 CPU @ 2.90GHz
BenchmarkApproach1-12 1000000 5423 ns/op 1024 B/op 1 allocs/op
BenchmarkApproach2-12 292579 20485 ns/op 5248 B/op 2 allocs/op
PASS
ok mytttttt 12.160s