github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/src/cmd/compile/internal/big/gcd_test.go (about) 1 // Copyright 2012 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 file implements a GCD benchmark. 6 // Usage: go test math/big -test.bench GCD 7 8 package big 9 10 import ( 11 "math/rand" 12 "testing" 13 ) 14 15 // randInt returns a pseudo-random Int in the range [1<<(size-1), (1<<size) - 1] 16 func randInt(r *rand.Rand, size uint) *Int { 17 n := new(Int).Lsh(intOne, size-1) 18 x := new(Int).Rand(r, n) 19 return x.Add(x, n) // make sure result > 1<<(size-1) 20 } 21 22 func runGCD(b *testing.B, aSize, bSize uint) { 23 b.StopTimer() 24 var r = rand.New(rand.NewSource(1234)) 25 aa := randInt(r, aSize) 26 bb := randInt(r, bSize) 27 b.StartTimer() 28 for i := 0; i < b.N; i++ { 29 new(Int).GCD(nil, nil, aa, bb) 30 } 31 } 32 33 func BenchmarkGCD10x10(b *testing.B) { runGCD(b, 10, 10) } 34 func BenchmarkGCD10x100(b *testing.B) { runGCD(b, 10, 100) } 35 func BenchmarkGCD10x1000(b *testing.B) { runGCD(b, 10, 1000) } 36 func BenchmarkGCD10x10000(b *testing.B) { runGCD(b, 10, 10000) } 37 func BenchmarkGCD10x100000(b *testing.B) { runGCD(b, 10, 100000) } 38 func BenchmarkGCD100x100(b *testing.B) { runGCD(b, 100, 100) } 39 func BenchmarkGCD100x1000(b *testing.B) { runGCD(b, 100, 1000) } 40 func BenchmarkGCD100x10000(b *testing.B) { runGCD(b, 100, 10000) } 41 func BenchmarkGCD100x100000(b *testing.B) { runGCD(b, 100, 100000) } 42 func BenchmarkGCD1000x1000(b *testing.B) { runGCD(b, 1000, 1000) } 43 func BenchmarkGCD1000x10000(b *testing.B) { runGCD(b, 1000, 10000) } 44 func BenchmarkGCD1000x100000(b *testing.B) { runGCD(b, 1000, 100000) } 45 func BenchmarkGCD10000x10000(b *testing.B) { runGCD(b, 10000, 10000) } 46 func BenchmarkGCD10000x100000(b *testing.B) { runGCD(b, 10000, 100000) } 47 func BenchmarkGCD100000x100000(b *testing.B) { runGCD(b, 100000, 100000) }