github.com/4ad/go@v0.0.0-20161219182952-69a12818b605/src/runtime/closure_test.go (about)

     1  // Copyright 2011 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  package runtime_test
     5  
     6  import "testing"
     7  
     8  var s int
     9  
    10  func BenchmarkCallClosure(b *testing.B) {
    11  	for i := 0; i < b.N; i++ {
    12  		s += func(ii int) int { return 2 * ii }(i)
    13  	}
    14  }
    15  
    16  func BenchmarkCallClosure1(b *testing.B) {
    17  	for i := 0; i < b.N; i++ {
    18  		j := i
    19  		s += func(ii int) int { return 2*ii + j }(i)
    20  	}
    21  }
    22  
    23  var ss *int
    24  
    25  func BenchmarkCallClosure2(b *testing.B) {
    26  	for i := 0; i < b.N; i++ {
    27  		j := i
    28  		s += func() int {
    29  			ss = &j
    30  			return 2
    31  		}()
    32  	}
    33  }
    34  
    35  func addr1(x int) *int {
    36  	return func() *int { return &x }()
    37  }
    38  
    39  func BenchmarkCallClosure3(b *testing.B) {
    40  	for i := 0; i < b.N; i++ {
    41  		ss = addr1(i)
    42  	}
    43  }
    44  
    45  func addr2() (x int, p *int) {
    46  	return 0, func() *int { return &x }()
    47  }
    48  
    49  func BenchmarkCallClosure4(b *testing.B) {
    50  	for i := 0; i < b.N; i++ {
    51  		_, ss = addr2()
    52  	}
    53  }