diff --git a/g/util/grand/grand_intn.go b/g/util/grand/grand_intn.go index effd207bd..935f68195 100644 --- a/g/util/grand/grand_intn.go +++ b/g/util/grand/grand_intn.go @@ -9,6 +9,7 @@ package grand import ( "crypto/rand" "encoding/binary" + "time" ) const ( @@ -21,14 +22,34 @@ var ( // 使用缓冲区实现快速的随机数生成 func init() { + step := 0 buffer := make([]byte, 1024) go func() { for { if n, err := rand.Read(buffer); err != nil { panic(err) } else { - for i := 0; i < n - 8; i += 8 { + // 使用缓冲区数据进行一次完整的随机数生成 + for i := 0; i < n - 8; { bufferChan <- binary.LittleEndian.Uint64(buffer[i : i + 8]) + i ++ + } + // 充分利用缓冲区数据,随机索引递增 + step = int(time.Now().UnixNano()%10) + for i := 0; i < n - 8; { + bufferChan <- binary.LittleEndian.Uint64(buffer[i : i + 8]) + i += step + } + // 充分利用缓冲区数据,字节倒序生成 + for i := 0; i < n - 8; { + bufferChan <- binary.BigEndian.Uint64(buffer[i : i + 8]) + i ++ + } + // 充分利用缓冲区数据,字节倒序生成,随机索引递增 + step = int(time.Now().UnixNano()%10) + for i := 0; i < n - 8; { + bufferChan <- binary.BigEndian.Uint64(buffer[i : i + 8]) + i += step } } } diff --git a/g/util/grand/grand_test.go b/g/util/grand/grand_test.go index c87323415..d76a79d90 100644 --- a/g/util/grand/grand_test.go +++ b/g/util/grand/grand_test.go @@ -9,12 +9,22 @@ package grand_test import ( - "testing" "gitee.com/johng/gf/g/util/grand" + "testing" ) +var buffer = make([]byte, 8) + func Benchmark_Rand(b *testing.B) { for i := 0; i < b.N; i++ { grand.Rand(0, 999999999) } } + +//func Benchmark_Buffer(b *testing.B) { +// for i := 0; i < b.N; i++ { +// if _, err := rand.Read(buffer); err == nil { +// binary.LittleEndian.Uint64(buffer) +// } +// } +//} diff --git a/geg/other/test.go b/geg/other/test.go index 86d4da0dd..9283f9a25 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -1,10 +1,23 @@ package main import ( - "fmt" - "gitee.com/johng/gf/g/os/gtime" + "gitee.com/johng/gf/g" + "gitee.com/johng/gf/g/net/ghttp" ) func main() { - fmt.Println(gtime.Now()) -} \ No newline at end of file + s := g.Server() + s.BindHandler("/", func(r *ghttp.Request) { + + }) + s.BindHandler("/user", func(r *ghttp.Request) { + + }) + s.BindHandler("/user/:id", func(r *ghttp.Request) { + r.Response.Header().Set("Content-Type", "text/plain; charset=utf-8") + r.Response.Write(r.Get("id")) + }) + s.SetFileServerEnabled(false) + s.SetPort(3000) + s.Run() +}