github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/runtime/rand_test.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package runtime_test
     6  
     7  import (
     8  	. "runtime"
     9  	"strconv"
    10  	"testing"
    11  )
    12  
    13  func BenchmarkFastrand(b *testing.B) {
    14  	b.RunParallel(func(pb *testing.PB) {
    15  		for pb.Next() {
    16  			Fastrand()
    17  		}
    18  	})
    19  }
    20  
    21  func BenchmarkFastrand64(b *testing.B) {
    22  	b.RunParallel(func(pb *testing.PB) {
    23  		for pb.Next() {
    24  			Fastrand64()
    25  		}
    26  	})
    27  }
    28  
    29  func BenchmarkFastrandHashiter(b *testing.B) {
    30  	var m = make(map[int]int, 10)
    31  	for i := 0; i < 10; i++ {
    32  		m[i] = i
    33  	}
    34  	b.RunParallel(func(pb *testing.PB) {
    35  		for pb.Next() {
    36  			for range m {
    37  				break
    38  			}
    39  		}
    40  	})
    41  }
    42  
    43  var sink32 uint32
    44  
    45  func BenchmarkFastrandn(b *testing.B) {
    46  	for n := uint32(2); n <= 5; n++ {
    47  		b.Run(strconv.Itoa(int(n)), func(b *testing.B) {
    48  			for i := 0; i < b.N; i++ {
    49  				sink32 = Fastrandn(n)
    50  			}
    51  		})
    52  	}
    53  }