github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/test/fixedbugs/bug369.go (about)

     1  // $G -N -o slow.$A $D/bug369.dir/pkg.go &&
     2  // $G -o fast.$A $D/bug369.dir/pkg.go &&
     3  // run
     4  
     5  // NOTE: This test is not run by 'run.go' and so not run by all.bash.
     6  // To run this test you must use the ./run shell script.
     7  
     8  // Copyright 2011 The Go Authors.  All rights reserved.
     9  // Use of this source code is governed by a BSD-style
    10  // license that can be found in the LICENSE file.
    11  
    12  // Test that compiling with optimization turned on produces faster code.
    13  
    14  package main
    15  
    16  import (
    17  	"flag"
    18  	"os"
    19  	"runtime"
    20  	"testing"
    21  
    22  	fast "./fast"
    23  	slow "./slow"
    24  )
    25  
    26  var buf = make([]byte, 1048576)
    27  
    28  func BenchmarkFastNonASCII(b *testing.B) {
    29  	for i := 0; i < b.N; i++ {
    30  		fast.NonASCII(buf, 0)
    31  	}
    32  }
    33  
    34  func BenchmarkSlowNonASCII(b *testing.B) {
    35  	for i := 0; i < b.N; i++ {
    36  		slow.NonASCII(buf, 0)
    37  	}
    38  }
    39  
    40  func main() {
    41  	os.Args = []string{os.Args[0], "-test.benchtime=100ms"}
    42  	flag.Parse()
    43  
    44  	rslow := testing.Benchmark(BenchmarkSlowNonASCII)
    45  	rfast := testing.Benchmark(BenchmarkFastNonASCII)
    46  	tslow := rslow.NsPerOp()
    47  	tfast := rfast.NsPerOp()
    48  
    49  	// Optimization should be good for at least 2x, but be forgiving.
    50  	// On the ARM simulator we see closer to 1.5x.
    51  	speedup := float64(tslow)/float64(tfast)
    52  	want := 1.8
    53  	if runtime.GOARCH == "arm" {
    54  		want = 1.3
    55  	}
    56  	if speedup < want {
    57  		// TODO(rsc): doesn't work on linux-amd64 or darwin-amd64 builders, nor on
    58  		// a Lenovo x200 (linux-amd64) laptop.
    59  		//println("fast:", tfast, "slow:", tslow, "speedup:", speedup, "want:", want)
    60  		//println("not fast enough")
    61  	}
    62  }