Skip to content

Instantly share code, notes, and snippets.

@ShotaKitazawa
Created November 27, 2021 08:42
Show Gist options
  • Select an option

  • Save ShotaKitazawa/cab5a4aeb3af7acff956ab1cf825b934 to your computer and use it in GitHub Desktop.

Select an option

Save ShotaKitazawa/cab5a4aeb3af7acff956ab1cf825b934 to your computer and use it in GitHub Desktop.
mapstructure 検証

検証内容

https://twitter.com/kanatakita/status/1464504836393615361 で気になったので、実際に測ってみる。

実行環境

  • x86_64
  • CentOS7 (3.10.0-1160.41.1.el7.x86_64)
  • Go 1.13.3 (該当マシンに入ってた以上の意味は特にない) / 1.17.3 (latest stable)

利用したコード

  • Unmarshal 対象のコードはキャッシュが効かないよう init にて rand で生成
package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"math/rand"
	"testing"

	"github.com/mitchellh/mapstructure"
)

type Struct struct {
	A int
	B int
	C int
}

var (
	m = []map[string]interface{}{}
)

const (
	loopCount = 1000000
)

func init() {
	for i := 0; i < loopCount; i++ {
		m = append(m, map[string]interface{}{
			"A": rand.Intn(10000000),
			"B": rand.Intn(10000000),
			"C": rand.Intn(10000000),
		})
	}
}

func BenchmarkMarshalAndUnmarshal(b *testing.B) {
	for i := 0; i < loopCount; i++ {
		s := Struct{}
		b, err := json.Marshal(m[i])
		if err != nil {
			panic(err)
		}
		if err := json.Unmarshal(b, &s); err != nil {
			panic(err)
		}
		fmt.Fprintln(ioutil.Discard, s)
	}
}

func BenchmarkMapstructure(b *testing.B) {
	for i := 0; i < loopCount; i++ {
		s := Struct{}
		if err := mapstructure.Decode(m[i], &s); err != nil {
			panic(err)
		}
		fmt.Fprintln(ioutil.Discard, s)
	}
}

実行結果

  • Go 1.13.3
$ go test -bench . -benchmem
goos: linux
goarch: amd64
pkg: tmp
BenchmarkMarshalAndUnmarshal-12                1        2717337540 ns/op        864013872 B/op  21000141 allocs/op
BenchmarkMapstructure-12                       1        2398854779 ns/op        1536013232 B/op 27000079 allocs/op
PASS
ok      tmp     5.760s
  • Go 1.17.3 (結果がブレるので4回実行)
$ go test -bench . -benchmem
goos: linux
goarch: amd64
pkg: tmp
cpu: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
BenchmarkMarshalAndUnmarshal-12                1        2356358364 ns/op        888012792 B/op  22000131 allocs/op
BenchmarkMapstructure-12                       1        2406090031 ns/op        1520010712 B/op 27000057 allocs/op
PASS
ok      tmp     5.320s

$ go test -bench . -benchmem
goos: linux
goarch: amd64
pkg: tmp
cpu: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
BenchmarkMarshalAndUnmarshal-12                1        2389570683 ns/op        888014376 B/op  22000152 allocs/op
BenchmarkMapstructure-12                       1        2273758160 ns/op        1520010408 B/op 27000054 allocs/op
PASS
ok      tmp     5.215s

$ go test -bench . -benchmem
goos: linux
goarch: amd64
pkg: tmp
cpu: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
BenchmarkMarshalAndUnmarshal-12                1        2367619790 ns/op        888014072 B/op  22000149 allocs/op
BenchmarkMapstructure-12                       1        2409974945 ns/op        1520009464 B/op 27000039 allocs/op
PASS
ok      tmp     5.372s

$ go test -bench . -benchmem
goos: linux
goarch: amd64
pkg: tmp
cpu: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
BenchmarkMarshalAndUnmarshal-12                1        2372604882 ns/op        888012984 B/op  22000134 allocs/op
BenchmarkMapstructure-12                       1        2260428026 ns/op        1520010472 B/op 27000054 allocs/op
PASS
ok      tmp     5.246s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment