-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathallocate_stack_vs_heap_test.go
More file actions
64 lines (54 loc) · 903 Bytes
/
Copy pathallocate_stack_vs_heap_test.go
File metadata and controls
64 lines (54 loc) · 903 Bytes
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
54
55
56
57
58
59
60
61
62
63
64
package main
import "testing"
type Foo struct {
foo int64
bar int64
baz int64
}
type Bar struct {
foo int64
bar int64
baz int64
bah int64
}
var bts []byte
func BenchmarkAllocateFooStack(b *testing.B) {
for i := 0; i < b.N; i++ {
func() Foo {
return Foo{}
}()
}
}
func BenchmarkAllocateBarStack(b *testing.B) {
for i := 0; i < b.N; i++ {
func() Bar {
return Bar{}
}()
}
}
func BenchmarkAllocateFooHeap(b *testing.B) {
for i := 0; i < b.N; i++ {
func() *Foo {
return new(Foo)
}()
}
}
func BenchmarkAllocateBarHeap(b *testing.B) {
for i := 0; i < b.N; i++ {
func() *Bar {
return new(Bar)
}()
}
}
func BenchmarkAllocateSliceHeapNoEscape(b *testing.B) {
for i := 0; i < b.N; i++ {
bts := make([]byte, 1024)
bts[0] = 1
}
}
func BenchmarkAllocateSliceHeapEscape(b *testing.B) {
for i := 0; i < b.N; i++ {
bts = make([]byte, 1024)
bts[0] = 1
}
}