github.com/chaowen112/go-lib@v0.0.0-20231018124935-124cd26d7cbe/randutils/shuffle_test.go (about)

     1  package randutils
     2  
     3  import (
     4  	"math/rand"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestRandShuffle(t *testing.T) {
    10  	a := []byte("0123456789abcdefghijklmnopqrstuvwxyz")
    11  	Shuffle(len(a), func(i, j int) {
    12  		a[i], a[j] = a[j], a[i]
    13  	})
    14  	t.Logf("%s", a)
    15  }
    16  
    17  func BenchmarkRandShuffleStd(b *testing.B) {
    18  	rand.Seed(time.Now().Unix())
    19  	b.SetParallelism(1000)
    20  	b.ReportAllocs()
    21  	b.ResetTimer()
    22  	b.RunParallel(func(pb *testing.PB) {
    23  		a := []byte("0123456789abcdefghijklmnopqrstuvwxyz")
    24  		for pb.Next() {
    25  			rand.Shuffle(len(a), func(i, j int) {
    26  				a[i], a[j] = a[j], a[i]
    27  			})
    28  		}
    29  	})
    30  }
    31  
    32  func BenchmarkRandShuffle(b *testing.B) {
    33  	b.SetParallelism(1000)
    34  	b.ReportAllocs()
    35  	b.ResetTimer()
    36  	b.RunParallel(func(pb *testing.PB) {
    37  		a := []byte("0123456789abcdefghijklmnopqrstuvwxyz")
    38  		for pb.Next() {
    39  			Shuffle(len(a), func(i, j int) {
    40  				a[i], a[j] = a[j], a[i]
    41  			})
    42  		}
    43  	})
    44  }