github.com/rsc/tmp@v0.0.0-20240517235954-6deaab19748b/bootstrap/internal/gc/big/gcd_test.go (about) 1 // Do not edit. Bootstrap copy of /Users/rsc/g/go/src/cmd/internal/gc/big/gcd_test.go 2 3 // Copyright 2012 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 // This file implements a GCD benchmark. 8 // Usage: go test math/big -test.bench GCD 9 10 package big 11 12 import ( 13 "math/rand" 14 "testing" 15 ) 16 17 // randInt returns a pseudo-random Int in the range [1<<(size-1), (1<<size) - 1] 18 func randInt(r *rand.Rand, size uint) *Int { 19 n := new(Int).Lsh(intOne, size-1) 20 x := new(Int).Rand(r, n) 21 return x.Add(x, n) // make sure result > 1<<(size-1) 22 } 23 24 func runGCD(b *testing.B, aSize, bSize uint) { 25 b.StopTimer() 26 var r = rand.New(rand.NewSource(1234)) 27 aa := randInt(r, aSize) 28 bb := randInt(r, bSize) 29 b.StartTimer() 30 for i := 0; i < b.N; i++ { 31 new(Int).GCD(nil, nil, aa, bb) 32 } 33 } 34 35 func BenchmarkGCD10x10(b *testing.B) { runGCD(b, 10, 10) } 36 func BenchmarkGCD10x100(b *testing.B) { runGCD(b, 10, 100) } 37 func BenchmarkGCD10x1000(b *testing.B) { runGCD(b, 10, 1000) } 38 func BenchmarkGCD10x10000(b *testing.B) { runGCD(b, 10, 10000) } 39 func BenchmarkGCD10x100000(b *testing.B) { runGCD(b, 10, 100000) } 40 func BenchmarkGCD100x100(b *testing.B) { runGCD(b, 100, 100) } 41 func BenchmarkGCD100x1000(b *testing.B) { runGCD(b, 100, 1000) } 42 func BenchmarkGCD100x10000(b *testing.B) { runGCD(b, 100, 10000) } 43 func BenchmarkGCD100x100000(b *testing.B) { runGCD(b, 100, 100000) } 44 func BenchmarkGCD1000x1000(b *testing.B) { runGCD(b, 1000, 1000) } 45 func BenchmarkGCD1000x10000(b *testing.B) { runGCD(b, 1000, 10000) } 46 func BenchmarkGCD1000x100000(b *testing.B) { runGCD(b, 1000, 100000) } 47 func BenchmarkGCD10000x10000(b *testing.B) { runGCD(b, 10000, 10000) } 48 func BenchmarkGCD10000x100000(b *testing.B) { runGCD(b, 10000, 100000) } 49 func BenchmarkGCD100000x100000(b *testing.B) { runGCD(b, 100000, 100000) }