From f6ea07cfc1364982652ec59401e33a2dfa22ea39 Mon Sep 17 00:00:00 2001 From: bddjr Date: Mon, 2 Dec 2024 17:07:05 +0800 Subject: [PATCH] optimize --- README.md | 42 +++++++++++++++++++++--------------------- rand.go | 14 +++++--------- 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 1b61f52..7b15e98 100644 --- a/README.md +++ b/README.md @@ -12,35 +12,35 @@ go get github.com/bddjr/cryptorandstr ``` MustRand10 6 - 961812 - 186418 - 476781 - 683053 - 189178 + 227679 + 901676 + 193607 + 026687 + 243505 MustRand16 27 - d4ff1e125208f0e2954001b3c6e - 36c517865848a7444454a7711ca - f5348bba35053bfeb962d3822b9 - df46ffa337732945d6c98ed0276 - 280a987f779f9775d8decffb150 + 017ced60ee75751a027605270fe + 2d89aa349e78988585524c5d074 + d155483394a0c8f75becd533faf + 7714135085cb7967f6ee645edb5 + 0d0e22c51fe5cad882d6a0867d7 MustRand32 27 - fjq0iakoubcl2sp92ekaq9ftsan - mgu2bms5d90ai35mtjdrru3rkfe - pt6k8rmsneovn3bq3q1ru2m26bk - 91rn0793voe4tupdcm2b31rfdd1 - ank42vo7ju49t0irpmmn65osj5p + 5e9hoqmhspbsnng2ct2hgosko4e + e9f5e7i5i24d9tlf1tef8fgcsba + 0dhkjpef6nqj1bnm81rhnjgjqjj + kj968dr8sc9srpk9roj3go45k8r + tvkb7pl542c3v5e3opqg5jcejhm MustRand64 27 - bMPa2jTkoJF4NzWgv4wr6Qmc-OK - _oWdmJSErNbW1rval-gM7ult-6_ - KhMddFuOkKCf9eHXx3s11H4AZmE - oCvBxMPnZwLV_Bi540RnH8_Nmk6 - st-G5VLmETyHV8WLhAOEaIWfuEn + xA-ZoFjHgQ927Jxnhly7WWRMvjw + NIBvnag5JrYhvHZ-NaKEZkmBgco + YD3qxeMe7AGlXllP0M07GkTXnMA + fC7_-XWOEf-BPv-U5_bRXrkEDqi + b1t6xZLgmW2v9KdxGEKqVIp-WAH PASS -ok github.com/bddjr/cryptorandstr 0.253s +ok github.com/bddjr/cryptorandstr 0.034s ``` ## License diff --git a/rand.go b/rand.go index b6cd1c4..523eca5 100644 --- a/rand.go +++ b/rand.go @@ -72,11 +72,12 @@ func Rand(strLen int, chars string) (string, error) { r := randWithGC{ outBitLen: bitLen(maxB), } + difference := 8 - r.outBitLen out := make([]byte, strLen) i := 0 for i < strLen { - b, err := r.byte(chunk) + b, err := r.byte(chunk, difference) if err != nil { return "", err } @@ -103,10 +104,7 @@ type randWithGC struct { cache uint16 } -// len(chunk) == 1 -func (r *randWithGC) byte(chunk []byte) (byte, error) { - difference := 8 - r.outBitLen - +func (r *randWithGC) byte(chunk []byte, difference byte) (byte, error) { // read cache if r.cacheBitLen >= r.outBitLen { b := (byte(r.cache) << difference) >> difference @@ -126,12 +124,10 @@ func (r *randWithGC) byte(chunk []byte) (byte, error) { } } - b := chunk[0] - // write cache r.cache <<= difference - r.cache |= uint16(b >> r.outBitLen) + r.cache |= uint16(chunk[0] >> r.outBitLen) r.cacheBitLen += difference - return (b << difference) >> difference, nil + return (chunk[0] << difference) >> difference, nil }