lo icon indicating copy to clipboard operation
lo copied to clipboard

refactor:refactor RandomString function

Open pigwantacat opened this issue 6 months ago • 0 comments

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

pigwantacat avatar Aug 22 '24 03:08 pigwantacat