Skip to content

Instantly share code, notes, and snippets.

@tiendc
Last active July 12, 2023 12:10
Show Gist options
  • Save tiendc/0a739fd880b9aac5373de95458d54808 to your computer and use it in GitHub Desktop.
Save tiendc/0a739fd880b9aac5373de95458d54808 to your computer and use it in GitHub Desktop.
go-deepcopy banchmark
package benchmark
import (
"testing"
"github.com/jinzhu/copier"
"github.com/tiendc/go-deepcopy"
"github.com/ulule/deepcopier"
)
type srcStruct2 struct {
S string
I int
I8 int8
I16 int16
I32 int32
I64 int64
U uint
U8 uint8
U16 uint16
U32 uint32
U64 uint64
F32 float32
F64 float64
B bool
}
type srcStruct1 struct {
S string
I int
I8 int8
I16 int16
I32 int32
I64 int64
U uint
U8 uint8
U16 uint16
U32 uint32
U64 uint64
F32 float32
F64 float64
B bool
SS []string
V srcStruct2
}
type dstStruct2 struct {
S string
I int
I8 int8
I16 int16
I32 int32
I64 int64
U uint
U8 uint8
U16 uint16
U32 uint32
U64 uint64
F32 float32
F64 float64
B bool
}
type dstStruct1 struct {
S string
I int
I8 int8
I16 int16
I32 int32
I64 int64
U uint
U8 uint8
U16 uint16
U32 uint32
U64 uint64
F32 float32
F64 float64
B bool
SS []string
V dstStruct2
}
var (
inStruct = srcStruct2{
S: "string",
I: 10,
I8: -8,
I16: -16,
I32: -32,
I64: -64,
U: 10,
U8: 8,
U16: 16,
U32: 32,
U64: 64,
F32: 32.0,
F64: 64.0,
B: true,
}
src = srcStruct1{
S: "string",
I: 10,
I8: -8,
I16: -16,
I32: -32,
I64: -64,
U: 10,
U8: 8,
U16: 16,
U32: 32,
U64: 64,
F32: 32.0,
F64: 64.0,
B: true,
SS: []string{"string1", "string2", "string3", "string4", "string5"},
V: inStruct,
}
)
func BenchmarkCopy(b *testing.B) {
b.Run("Go-DeepCopy", func(b *testing.B) {
for i := 0; i < b.N; i++ {
var dst dstStruct1
_ = deepcopy.Copy(&dst, src)
}
})
b.Run("ManualCopy", func(b *testing.B) {
for i := 0; i < b.N; i++ {
var dst dstStruct1
dst.S = src.S
dst.I = src.I
dst.I8 = src.I8
dst.I16 = src.I16
dst.I32 = src.I32
dst.I64 = src.I64
dst.U = src.U
dst.U8 = src.U8
dst.U16 = src.U16
dst.U32 = src.U32
dst.U64 = src.U64
dst.F32 = src.F32
dst.F64 = src.F64
dst.B = src.B
dst.SS = append(dst.SS, src.SS...)
dst.V.S = src.V.S
dst.V.I = src.V.I
dst.V.I8 = src.V.I8
dst.V.I16 = src.V.I16
dst.V.I32 = src.V.I32
dst.V.I64 = src.V.I64
dst.V.U = src.V.U
dst.V.U8 = src.V.U8
dst.V.U16 = src.V.U16
dst.V.U32 = src.V.U32
dst.V.U64 = src.V.U64
dst.V.F32 = src.V.F32
dst.V.F64 = src.V.F64
dst.V.B = src.V.B
}
})
b.Run("JinzhuCopier", func(b *testing.B) {
for i := 0; i < b.N; i++ {
var dst dstStruct1
_ = copier.Copy(&dst, &src)
}
})
b.Run("Deepcopier", func(b *testing.B) {
for i := 0; i < b.N; i++ {
var dst dstStruct1
_ = deepcopier.Copy(&src).To(&dst)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment