Source file src/runtime/closure_test.go

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

View as plain text