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倍,以及额外的内存消耗。

package eudore_test

import (
    "encoding/json"
    "testing"
)

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"`
}

func BenchmarkFor(b *testing.B) {
    b.ReportAllocs()
    var a A = A{"aaa", "bbb"}
    var bb B
    for i := 0; i < b.N; i++ {
        bb.ContentA = a.ContentA
        bb.ContentB = a.ContentB
    }
}

func BenchmarkJSON(b *testing.B) {
    b.ReportAllocs()
    var a A = A{"aaa", "bbb"}
    var bb B
    for i := 0; i < b.N; i++ {
        body, _ := json.Marshal(&a)
        json.Unmarshal(body, &bb)
    }
}
[root@localhost test]# go test -bench=. 01_test.go 
goos: linux
goarch: amd64
BenchmarkFor-2        2000000000             0.64 ns/op           0 B/op           0 allocs/op
BenchmarkJSON-2         500000          3126 ns/op         480 B/op           5 allocs/op
PASS
ok      command-line-arguments    2.963s
3年前 评论
讨论数量: 1

当然是for的性能好,使用json marshal和unmarshal需要反射操作,性能低很多,可以写个benchmakr测试下。

下面是benchmark的代码和结果,得出结论json修改需要时间的赋值的5000倍,以及额外的内存消耗。

package eudore_test

import (
    "encoding/json"
    "testing"
)

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"`
}

func BenchmarkFor(b *testing.B) {
    b.ReportAllocs()
    var a A = A{"aaa", "bbb"}
    var bb B
    for i := 0; i < b.N; i++ {
        bb.ContentA = a.ContentA
        bb.ContentB = a.ContentB
    }
}

func BenchmarkJSON(b *testing.B) {
    b.ReportAllocs()
    var a A = A{"aaa", "bbb"}
    var bb B
    for i := 0; i < b.N; i++ {
        body, _ := json.Marshal(&a)
        json.Unmarshal(body, &bb)
    }
}
[root@localhost test]# go test -bench=. 01_test.go 
goos: linux
goarch: amd64
BenchmarkFor-2        2000000000             0.64 ns/op           0 B/op           0 allocs/op
BenchmarkJSON-2         500000          3126 ns/op         480 B/op           5 allocs/op
PASS
ok      command-line-arguments    2.963s
3年前 评论

当然是for的性能好,使用json marshal和unmarshal需要反射操作,性能低很多,可以写个benchmakr测试下。

下面是benchmark的代码和结果,得出结论json修改需要时间的赋值的5000倍,以及额外的内存消耗。

package eudore_test

import (
    "encoding/json"
    "testing"
)

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"`
}

func BenchmarkFor(b *testing.B) {
    b.ReportAllocs()
    var a A = A{"aaa", "bbb"}
    var bb B
    for i := 0; i < b.N; i++ {
        bb.ContentA = a.ContentA
        bb.ContentB = a.ContentB
    }
}

func BenchmarkJSON(b *testing.B) {
    b.ReportAllocs()
    var a A = A{"aaa", "bbb"}
    var bb B
    for i := 0; i < b.N; i++ {
        body, _ := json.Marshal(&a)
        json.Unmarshal(body, &bb)
    }
}
[root@localhost test]# go test -bench=. 01_test.go 
goos: linux
goarch: amd64
BenchmarkFor-2        2000000000             0.64 ns/op           0 B/op           0 allocs/op
BenchmarkJSON-2         500000          3126 ns/op         480 B/op           5 allocs/op
PASS
ok      command-line-arguments    2.963s
3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!