Golang

关注公众号 jb51net

关闭
首页 > 脚本专栏 > Golang > 几种深度拷贝(deepcopy)方法性能对比

Go语言关于几种深度拷贝(deepcopy)方法的性能对比

作者:pengpengzhou

这篇文章主要介绍了Go语言关于几种深度拷贝(deepcopy)方法的性能对比,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

几种深度拷贝(deepcopy)方法性能对比

Go语言中所有赋值操作都是值传递,如果结构中不含指针,则直接赋值就是深度拷贝;如果结构中含有指针(包括自定义指针,以及切片,map等使用了指针的内置类型),则数据源和拷贝之间对应指针会共同指向同一块内存,这时深度拷贝需要特别处理。

目前有三种方法

前两种方法虽然比较通用但是因为使用了reflex反射,性能比定制化拷贝要低出2个数量级,所以在性能要求较高的情况下应该尽量避免使用前两者。

结论数据

执行一次的时间

gob time:454µs
json time:170µs
custom time:2µs

测试代码如下:

package main
 
import (
	"bytes"
	"encoding/gob"
	"encoding/json"
	"fmt"
	"time"
)
 
type AuthorInfo struct {
	Name    string `json:name`
	Age     int    `json:age`
	Country *int   `json:country`
}
 
type Book struct {
	Title    string            `json:title`
	Author   AuthorInfo        `json:author`
	Year     int               `json:year`
	Category []string          `json:category`
	Price    map[string]string `json:price`
}
 
func DeepCopyByGob(dst, src interface{}) error {
	var buffer bytes.Buffer
	if err := gob.NewEncoder(&buffer).Encode(src); err != nil {
		return err
	}
 
	return gob.NewDecoder(&buffer).Decode(dst)
}
 
func DeepCopyByJson(src []Book) (*[]Book, error) {
	var dst = new([]Book)
	b, err := json.Marshal(src)
	if err != nil {
		return nil, err
	}
 
	err = json.Unmarshal(b, dst)
	return dst, err
}
 
func DeepCopyByCustom(src []Book) []Book {
	dst := make([]Book, len(src))
	for i, book := range src {
		tmpbook := Book{}
		tmpbook.Title = book.Title
		tmpbook.Year = book.Year
		tmpbook.Author = AuthorInfo{}
		tmpbook.Author.Name = book.Author.Name
		tmpbook.Author.Age = book.Author.Age
		tmpbook.Author.Country = new(int)
		*tmpbook.Author.Country = *book.Author.Country
		tmpbook.Category = make([]string, len(book.Category))
		for index, category := range book.Category {
			tmpbook.Category[index] = category
		}
		tmpbook.Price = make(map[string]string)
		for k, v := range book.Price {
			tmpbook.Price[k] = v
		}
		dst[i] = tmpbook
	}
	return dst
}
 
func check(err error){
	if err != nil{
		panic(err)
	}
}
 
func print(name string, books []Book){
	for index,book := range books{
		fmt.Printf("%s[%d]=%v country=%d\n", name, index, book, *book.Author.Country)
	}
}
 
func main() {
	//初始化源Book切片
	books := make([]Book, 1)
	country := 1156
	author := AuthorInfo{"David", 38, &country}
	price := make(map[string]string)
	price["Europe"] = "$56"
	books[0] = Book{"Tutorial", author, 2020, []string{"math", "art"}, price}
	print("books",books)
 
	var err error
	var start time.Time
 
	//Gob拷贝
	start = time.Now()
	booksCpy := make([]Book, 1)
	err = DeepCopyByGob(&booksCpy, books)
	fmt.Printf("\ngob time:%v\n", time.Now().Sub(start))
	check(err)
	*booksCpy[0].Author.Country = 1134
	booksCpy[0].Category[0] = "literature"
	booksCpy[0].Price["America"] = "$250"
	print("booksCpy",booksCpy)
	print("books",books)
 
	//JSON拷贝
	start = time.Now()
	booksCpy2, err_json := DeepCopyByJson(books)
	fmt.Printf("\njson time:%v\n", time.Now().Sub(start))
	check(err_json)
	*(*booksCpy2)[0].Author.Country = 1135
	(*booksCpy2)[0].Category[0] = "science"
	(*booksCpy2)[0].Price["Canada"] = "$150"
	print("(*booksCpy2)",*booksCpy2)
	print("books",books)
 
	//定制拷贝
	start = time.Now()
	booksCpy3 := DeepCopyByCustom(books)
	fmt.Printf("\ncustom time:%v\n", time.Now().Sub(start))
	*booksCpy3[0].Author.Country = 1136
	booksCpy3[0].Category[0] = "geometry"
	booksCpy3[0].Price["Africa"] = "$34"
	print("booksCpy3",booksCpy3)
	print("books",books)
}

运行输出:

books[0]={Tutorial {David 38 0xc000016178} 2020 [math art] map[Europe:$56]} country=1156
 
gob time:454.117µs
booksCpy[0]={Tutorial {David 38 0xc0000165d8} 2020 [literature art] map[America:$250 Europe:$56]} country=1134
books[0]={Tutorial {David 38 0xc000016178} 2020 [math art] map[Europe:$56]} country=1156
 
json time:170.338µs
(*booksCpy2)[0]={Tutorial {David 38 0xc000016878} 2020 [science art] map[Canada:$150 Europe:$56]} country=1135
books[0]={Tutorial {David 38 0xc000016178} 2020 [math art] map[Europe:$56]} country=1156
 
custom time:2.165µs
booksCpy3[0]={Tutorial {David 38 0xc0000168c8} 2020 [geometry art] map[Africa:$34 Europe:$56]} country=1136
books[0]={Tutorial {David 38 0xc000016178} 2020 [math art] map[Europe:$56]} country=1156

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文