pgregory.net/rand@v1.0.3-0.20230808192358-a0b8ce02f4da/rand_bench_x_test.go (about)

     1  // Copyright 2022 Gregory Petrosyan <gregory.petrosyan@gmail.com>
     2  //
     3  // This Source Code Form is subject to the terms of the Mozilla Public
     4  // License, v. 2.0. If a copy of the MPL was not distributed with this
     5  // file, You can obtain one at https://mozilla.org/MPL/2.0/.
     6  
     7  //go:build benchx
     8  
     9  package rand_test
    10  
    11  import (
    12  	"golang.org/x/exp/rand"
    13  	"math"
    14  	"testing"
    15  )
    16  
    17  var (
    18  	sinkRand *rand.Rand
    19  )
    20  
    21  func BenchmarkUint64(b *testing.B) {
    22  	b.RunParallel(func(pb *testing.PB) {
    23  		var s uint64
    24  		b.SetBytes(8)
    25  		for pb.Next() {
    26  			s = rand.Uint64()
    27  		}
    28  		sinkUint64 = s
    29  	})
    30  }
    31  
    32  func BenchmarkFloat64(b *testing.B) {
    33  	b.RunParallel(func(pb *testing.PB) {
    34  		var s float64
    35  		for pb.Next() {
    36  			s = rand.Float64()
    37  		}
    38  		sinkFloat64 = s
    39  	})
    40  }
    41  
    42  func BenchmarkIntn(b *testing.B) {
    43  	b.RunParallel(func(pb *testing.PB) {
    44  		var s int
    45  		for pb.Next() {
    46  			s = rand.Intn(small)
    47  		}
    48  		sinkInt = s
    49  	})
    50  }
    51  
    52  func BenchmarkIntn_Big(b *testing.B) {
    53  	b.RunParallel(func(pb *testing.PB) {
    54  		var s int
    55  		for pb.Next() {
    56  			s = rand.Intn(math.MaxInt - small)
    57  		}
    58  		sinkInt = s
    59  	})
    60  }
    61  
    62  func BenchmarkRand_New(b *testing.B) {
    63  	var s *rand.Rand
    64  	b.ReportAllocs()
    65  	for i := 0; i < b.N; i++ {
    66  		s = rand.New(rand.NewSource(uint64(i)))
    67  	}
    68  	sinkRand = s
    69  }
    70  
    71  func BenchmarkRand_ExpFloat64(b *testing.B) {
    72  	var s float64
    73  	r := rand.New(rand.NewSource(1))
    74  	for i := 0; i < b.N; i++ {
    75  		s = r.ExpFloat64()
    76  	}
    77  	sinkFloat64 = s
    78  }
    79  
    80  func BenchmarkRand_Float32(b *testing.B) {
    81  	var s float32
    82  	r := rand.New(rand.NewSource(1))
    83  	for i := 0; i < b.N; i++ {
    84  		s = r.Float32()
    85  	}
    86  	sinkFloat32 = s
    87  }
    88  
    89  func BenchmarkRand_Float64(b *testing.B) {
    90  	var s float64
    91  	r := rand.New(rand.NewSource(1))
    92  	for i := 0; i < b.N; i++ {
    93  		s = r.Float64()
    94  	}
    95  	sinkFloat64 = s
    96  }
    97  
    98  func BenchmarkRand_Int(b *testing.B) {
    99  	var s int
   100  	r := rand.New(rand.NewSource(1))
   101  	for i := 0; i < b.N; i++ {
   102  		s = r.Int()
   103  	}
   104  	sinkInt = s
   105  }
   106  
   107  func BenchmarkRand_Int31(b *testing.B) {
   108  	var s int32
   109  	r := rand.New(rand.NewSource(1))
   110  	for i := 0; i < b.N; i++ {
   111  		s = r.Int31()
   112  	}
   113  	sinkInt32 = s
   114  }
   115  
   116  func BenchmarkRand_Int31n(b *testing.B) {
   117  	var s int32
   118  	r := rand.New(rand.NewSource(1))
   119  	for i := 0; i < b.N; i++ {
   120  		s = r.Int31n(small)
   121  	}
   122  	sinkInt32 = s
   123  }
   124  
   125  func BenchmarkRand_Int31n_Big(b *testing.B) {
   126  	var s int32
   127  	r := rand.New(rand.NewSource(1))
   128  	for i := 0; i < b.N; i++ {
   129  		s = r.Int31n(math.MaxInt32 - small)
   130  	}
   131  	sinkInt32 = s
   132  }
   133  
   134  func BenchmarkRand_Int63(b *testing.B) {
   135  	var s int64
   136  	r := rand.New(rand.NewSource(1))
   137  	for i := 0; i < b.N; i++ {
   138  		s = r.Int63()
   139  	}
   140  	sinkInt64 = s
   141  }
   142  
   143  func BenchmarkRand_Int63n(b *testing.B) {
   144  	var s int64
   145  	r := rand.New(rand.NewSource(1))
   146  	for i := 0; i < b.N; i++ {
   147  		s = r.Int63n(small)
   148  	}
   149  	sinkInt64 = s
   150  }
   151  
   152  func BenchmarkRand_Int63n_Big(b *testing.B) {
   153  	var s int64
   154  	r := rand.New(rand.NewSource(1))
   155  	for i := 0; i < b.N; i++ {
   156  		s = r.Int63n(math.MaxInt64 - small)
   157  	}
   158  	sinkInt64 = s
   159  }
   160  
   161  func BenchmarkRand_Intn(b *testing.B) {
   162  	var s int
   163  	r := rand.New(rand.NewSource(1))
   164  	for i := 0; i < b.N; i++ {
   165  		s = r.Intn(small)
   166  	}
   167  	sinkInt = s
   168  }
   169  
   170  func BenchmarkRand_Intn_Big(b *testing.B) {
   171  	var s int
   172  	r := rand.New(rand.NewSource(1))
   173  	for i := 0; i < b.N; i++ {
   174  		s = r.Intn(math.MaxInt - small)
   175  	}
   176  	sinkInt = s
   177  }
   178  
   179  func BenchmarkRand_NormFloat64(b *testing.B) {
   180  	var s float64
   181  	r := rand.New(rand.NewSource(1))
   182  	for i := 0; i < b.N; i++ {
   183  		s = r.NormFloat64()
   184  	}
   185  	sinkFloat64 = s
   186  }
   187  
   188  func BenchmarkRand_Perm(b *testing.B) {
   189  	b.ReportAllocs()
   190  	r := rand.New(rand.NewSource(1))
   191  	for i := 0; i < b.N; i++ {
   192  		r.Perm(tiny)
   193  	}
   194  }
   195  
   196  func BenchmarkRand_Read(b *testing.B) {
   197  	r := rand.New(rand.NewSource(1))
   198  	p := make([]byte, 256)
   199  	b.SetBytes(int64(len(p)))
   200  	for i := 0; i < b.N; i++ {
   201  		_, _ = r.Read(p[:])
   202  	}
   203  }
   204  
   205  func BenchmarkRand_Seed(b *testing.B) {
   206  	r := rand.New(rand.NewSource(1))
   207  	for i := 0; i < b.N; i++ {
   208  		r.Seed(uint64(i))
   209  	}
   210  }
   211  
   212  func BenchmarkRand_Shuffle(b *testing.B) {
   213  	r := rand.New(rand.NewSource(1))
   214  	a := make([]int, tiny)
   215  	for i := 0; i < b.N; i++ {
   216  		r.Shuffle(len(a), func(i, j int) { a[i], a[j] = a[j], a[i] })
   217  	}
   218  }
   219  
   220  func BenchmarkRand_ShuffleOverhead(b *testing.B) {
   221  	r := rand.New(rand.NewSource(1))
   222  	a := make([]int, tiny)
   223  	for i := 0; i < b.N; i++ {
   224  		r.Shuffle(len(a), func(i, j int) {})
   225  	}
   226  }
   227  
   228  func BenchmarkRand_Uint32(b *testing.B) {
   229  	var s uint32
   230  	r := rand.New(rand.NewSource(1))
   231  	b.SetBytes(4)
   232  	for i := 0; i < b.N; i++ {
   233  		s = r.Uint32()
   234  	}
   235  	sinkUint32 = s
   236  }
   237  
   238  func BenchmarkRand_Uint64(b *testing.B) {
   239  	var s uint64
   240  	r := rand.New(rand.NewSource(1))
   241  	b.SetBytes(8)
   242  	for i := 0; i < b.N; i++ {
   243  		s = r.Uint64()
   244  	}
   245  	sinkUint64 = s
   246  }
   247  
   248  func BenchmarkRand_Uint64n(b *testing.B) {
   249  	var s uint64
   250  	r := rand.New(rand.NewSource(1))
   251  	for i := 0; i < b.N; i++ {
   252  		s = r.Uint64n(small)
   253  	}
   254  	sinkUint64 = s
   255  }
   256  
   257  func BenchmarkRand_Uint64n_Big(b *testing.B) {
   258  	var s uint64
   259  	r := rand.New(rand.NewSource(1))
   260  	for i := 0; i < b.N; i++ {
   261  		s = r.Uint64n(math.MaxUint64 - small)
   262  	}
   263  	sinkUint64 = s
   264  }
   265  
   266  func BenchmarkRand_MarshalBinary(b *testing.B) {
   267  	b.ReportAllocs()
   268  	r := rand.NewSource(1).(*rand.PCGSource)
   269  	for i := 0; i < b.N; i++ {
   270  		_, _ = r.MarshalBinary()
   271  	}
   272  }
   273  
   274  func BenchmarkRand_UnmarshalBinary(b *testing.B) {
   275  	b.ReportAllocs()
   276  	r := rand.NewSource(1).(*rand.PCGSource)
   277  	buf, _ := r.MarshalBinary()
   278  	for i := 0; i < b.N; i++ {
   279  		_ = r.UnmarshalBinary(buf)
   280  	}
   281  }