github.com/rsc/tmp@v0.0.0-20240517235954-6deaab19748b/bound/bound_test.go (about)

     1  // Copyright 2014 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  // This simple program benchmarks the BOUND instruction
     6  // against an explicit CMP and conditional jump.
     7  // It must be compiled for 386, since BOUND is not available
     8  // on amd64.
     9  
    10  package bound
    11  
    12  import "testing"
    13  
    14  func bound(int32)
    15  func cmp(int32)
    16  func cmpreg(int32)
    17  func nop(int32)
    18  
    19  func bench(b *testing.B, f func(int32)) {
    20  	x := int(b.N / 1e9)
    21  	for i := 0; i < x; i++ {
    22  		f(1e9)
    23  	}
    24  	f(int32(b.N % 1e9))
    25  }
    26  
    27  func BenchmarkNop(b *testing.B) { bench(b, nop) }
    28  func BenchmarkBound(b *testing.B) { bench(b, bound) }
    29  func BenchmarkCmp(b *testing.B) { bench(b, cmp) }
    30  func BenchmarkCmpReg(b *testing.B) { bench(b, cmpreg) }