-
Notifications
You must be signed in to change notification settings - Fork 1
/
go-deepcopy-benchmark.go
93 lines (80 loc) · 1.81 KB
/
go-deepcopy-benchmark.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"bytes"
"encoding/gob"
"encoding/json"
"log"
"time"
"strconv"
)
// Test ...
type Test struct {
Prop1 int
Prop2 string
}
// Clone deep-copies a to b
func Clone(a, b interface{}) {
buff := new(bytes.Buffer)
enc := gob.NewEncoder(buff)
dec := gob.NewDecoder(buff)
enc.Encode(a)
dec.Decode(b)
}
// DeepCopy deepcopies a to b using json marshaling
func DeepCopy(a, b interface{}) {
byt, _ := json.Marshal(a)
json.Unmarshal(byt, b)
}
// ClonePointer deepcopies a to b using json marshaling
func ClonePointer(a, b *Test) {
b = &Test{
Prop1: a.Prop1,
Prop2: a.Prop2,
}
}
// CloneNonPointer deepcopies a to b using json marshaling
func CloneNonPointer(a, b Test) {
b = Test{
Prop1: a.Prop1,
Prop2: a.Prop2,
}
}
func main() {
i := 0
tClone := time.Duration(0)
tCopy := time.Duration(0)
tClonePointer := time.Duration(0)
tCloneNonPointer := time.Duration(0)
round := 100000
for {
if i == round {
break
}
r := Test{Prop1: i, Prop2: strconv.Itoa(i)}
var rNew Test
t0 := time.Now()
Clone(r, &rNew)
t2 := time.Now().Sub(t0)
tClone += t2
r2 := Test{Prop1: i, Prop2: strconv.Itoa(i)}
var rNew2 Test
t0 = time.Now()
DeepCopy(&r2, &rNew2)
t2 = time.Now().Sub(t0)
tCopy += t2
r3 := &Test{Prop1: i, Prop2: strconv.Itoa(i)}
var rNew3 *Test
t0 = time.Now()
ClonePointer(r3, rNew3)
t2 = time.Now().Sub(t0)
tClonePointer += t2
r4 := Test{Prop1: i, Prop2: strconv.Itoa(i)}
var rNew4 Test
t0 = time.Now()
CloneNonPointer(r4, rNew4)
t2 = time.Now().Sub(t0)
tCloneNonPointer += t2
i++
}
log.Printf("Total items %+v, Clone avg. %+v, DeepCopy avg. %+v, ClonePointer avg. %+v, CloneNonPointer avg. %+v\n", i, int(tClone.Nanoseconds())/round, int(tCopy.Nanoseconds())/round, int(tClonePointer.Nanoseconds())/round, int(tCloneNonPointer.Nanoseconds())/round)
}