github.com/andy-kimball/arenaskl@v0.0.0-20200617143215-f701008588b9/internal/fastrand/fastrand_test.go (about)

     1  /*
     2   * Copyright 2017 Dgraph Labs, Inc. and Contributors
     3   * Modifications copyright (C) 2020 Andy Kimball and Contributors
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package fastrand
    19  
    20  import (
    21  	"math/rand"
    22  	"testing"
    23  )
    24  
    25  // Results with go1.13.12 on a Mac with a 2.3 GHz 8-Core Intel Core i9 processor:
    26  //
    27  // $ go test -cpu 1,2,4,8,16,32 -bench=. -count=1 | benchstat /dev/stdin
    28  //
    29  //   name            time/op
    30  //   FastRand        2.25ns ± 2%
    31  //   FastRand-2      1.14ns ± 3%
    32  //   FastRand-4      0.59ns ± 4%
    33  //   FastRand-8      0.35ns ± 6%
    34  //   FastRand-16     0.33ns ± 4%
    35  //   FastRand-32     0.32ns ± 2%
    36  //   DefaultRand     13.3ns ± 3%
    37  //   DefaultRand-2   19.2ns ± 3%
    38  //   DefaultRand-4   41.5ns ± 1%
    39  //   DefaultRand-8   56.7ns ± 1%
    40  //   DefaultRand-16  66.6ns ±12%
    41  //   DefaultRand-32  65.8ns ± 6%
    42  //
    43  
    44  func BenchmarkFastRand(b *testing.B) {
    45  	b.RunParallel(func(pb *testing.PB) {
    46  		for pb.Next() {
    47  			Uint32()
    48  		}
    49  	})
    50  }
    51  
    52  func BenchmarkDefaultRand(b *testing.B) {
    53  	b.RunParallel(func(pb *testing.PB) {
    54  		for pb.Next() {
    55  			rand.Uint32()
    56  		}
    57  	})
    58  }