json.unmarshal for 哪个效率高
将structA的值赋值给structB使用for循环还是使用json.marshal和unmarshal效率高呢?
type A struct {
ContentA string `json:"content_a"`
ContentB string `json:"content_b"`
}
type B struct {
ContentA string `json:"content_a"`
ContentB string `json:"content_b"`
}
当然是for的性能好,使用json marshal和unmarshal需要反射操作,性能低很多,可以写个benchmakr测试下。
下面是benchmark的代码和结果,得出结论json修改需要时间的赋值的5000倍,以及额外的内存消耗。