1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package benchmark_go_closures

import (
	"testing"
	"fmt"
)

func BenchmarkAnonymousFunction(b *testing.B) {
	b.StopTimer()
	var t int64
	f := func(j int64) (r int64) {
		r = j * 2
		return
	}
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		t = f(int64(i))
	}
	b.StopTimer()
	fmt.Sprintln(t)
}

func BenchmarkClosure(b *testing.B) {
	b.StopTimer()
	var t int64
	var i int
        f := func() (r int64) {
		r = int64(i) * 2
		return
        }
	b.StartTimer()
	for i = 0; i < b.N; i++ {
		t = f()
	}
	b.StopTimer()
	fmt.Sprintln(t)
}

func BenchmarkNormalFunction(b *testing.B) {
	b.StopTimer()
	var t int64
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		t = multiple2(int64(i))
	}
	b.StopTimer()
	fmt.Sprintln(t)
}

func multiple2(i int64) (r int64) {
    r = i * 2
    return
}