2019-02-02 16:18:25 +08:00
|
|
|
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2018-04-27 11:38:26 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-04-27 11:38:26 +08:00
|
|
|
|
|
|
|
// go test *.go -bench=".*"
|
|
|
|
|
|
|
|
package grand_test
|
|
|
|
|
|
|
|
import (
|
2019-11-08 19:52:49 +08:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/binary"
|
2019-06-19 09:06:52 +08:00
|
|
|
"testing"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
|
|
|
"github.com/gogf/gf/util/grand"
|
2018-04-27 11:38:26 +08:00
|
|
|
)
|
|
|
|
|
2018-11-23 21:39:05 +08:00
|
|
|
var buffer = make([]byte, 8)
|
|
|
|
|
2020-03-30 20:31:47 +08:00
|
|
|
func Benchmark_Rand_Intn(b *testing.B) {
|
2020-02-21 00:01:27 +08:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
grand.N(0, 99)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 20:31:47 +08:00
|
|
|
func Benchmark_Perm10(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
grand.Perm(10)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Benchmark_Perm100(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
grand.Perm(100)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Benchmark_Rand_N1(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
grand.N(0, 99)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Benchmark_Rand_N2(b *testing.B) {
|
2019-06-19 09:06:52 +08:00
|
|
|
for i := 0; i < b.N; i++ {
|
2019-09-06 17:59:55 +08:00
|
|
|
grand.N(0, 999999999)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2018-04-27 11:38:26 +08:00
|
|
|
}
|
2018-11-23 21:39:05 +08:00
|
|
|
|
2020-02-21 00:01:27 +08:00
|
|
|
func Benchmark_Str(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
grand.S(16)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Benchmark_StrSymbols(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
grand.S(16, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-16 16:05:31 +08:00
|
|
|
func Benchmark_Uint32Converting(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
binary.LittleEndian.Uint32([]byte{1, 1, 1, 1})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-08 19:52:49 +08:00
|
|
|
func Benchmark_Buffer(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if _, err := rand.Read(buffer); err == nil {
|
|
|
|
binary.LittleEndian.Uint64(buffer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|