-
Notifications
You must be signed in to change notification settings - Fork 1
/
few_test.go
52 lines (47 loc) · 1.35 KB
/
few_test.go
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
package main
import (
"encoding/json"
"reflect"
"testing"
"unsafe"
)
// BenchmarkCastSmall-4 2000000000 1.65 ns/op 0 B/op 0 allocs/op
func BenchmarkCastSmall(b *testing.B) {
b.ReportAllocs()
var x int
for i := 0; i < b.N; i++ {
_ = *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{uintptr(unsafe.Pointer(&x)), int(unsafe.Sizeof(x)), int(unsafe.Sizeof(x))}))
}
}
// BenchmarkCastLarge-4 2000000000 1.67 ns/op 0 B/op 0 allocs/op
func BenchmarkCastLarge(b *testing.B) {
b.ReportAllocs()
var x [1000]int
for i := 0; i < b.N; i++ {
_ = *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{uintptr(unsafe.Pointer(&x)), int(unsafe.Sizeof(x)), int(unsafe.Sizeof(x))}))
}
}
// BenchmarkCastPtr-4 1000000 1686 ns/op 0 B/op 0 allocs/op
func BenchmarkCastPtr(b *testing.B) {
b.ReportAllocs()
var x [1000]*int
for i := range x {
x[i] = new(int)
}
for i := 0; i < b.N; i++ {
for j := range x {
_ = *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{uintptr(unsafe.Pointer(x[j])), int(unsafe.Sizeof(x[j])), int(unsafe.Sizeof(x[j]))}))
}
}
}
// BenchmarkJSON-4 30000 57213 ns/op 12840 B/op 8 allocs/op
func BenchmarkJSON(b *testing.B) {
b.ReportAllocs()
var x [1000]*int
for i := range x {
x[i] = new(int)
}
for i := 0; i < b.N; i++ {
json.Marshal(x)
}
}