Skip to content

Instantly share code, notes, and snippets.

@c1982
Last active December 27, 2020 14:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c1982/2b36f310c6172c9b74f37da63ce670a8 to your computer and use it in GitHub Desktop.
Save c1982/2b36f310c6172c9b74f37da63ce670a8 to your computer and use it in GitHub Desktop.
Benchmarks for inline, outline, anonymous struct
package main
import (
"encoding/json"
"testing"
)
type PD2 struct {
Property1 int `json:"property1"`
Property2 int `json:"property2"`
Property3 int `json:"property3"`
Property4 float64 `json:"property4"`
Property5 float64 `json:"property5"`
Property6 bool `json:"property6"`
}
func Benchmark_Struct(b *testing.B) {
data := []byte(`{"property1":1, "property2":1, "property3":3, "property4":"5", "property5":"0.4", "property6":true}`)
b.Run("inline_struct", func(b *testing.B) {
for n := 0; n < b.N; n++ {
type PD2 struct {
Property1 int `json:"property1"`
Property2 int `json:"property2"`
Property3 int `json:"property3"`
Property4 float64 `json:"property4"`
Property5 float64 `json:"property5"`
Property6 bool `json:"property6"`
}
var pd2 *PD2
_ = json.Unmarshal(data, &pd2)
}
})
b.Run("outline_struct_inloop", func(b *testing.B) {
for n := 0; n < b.N; n++ {
var pd2 *PD2
_ = json.Unmarshal(data, &pd2)
}
})
b.Run("outline_struct_outloop", func(b *testing.B) {
var pd2 *PD2
for n := 0; n < b.N; n++ {
_ = json.Unmarshal(data, &pd2)
}
})
b.Run("inline_struct_anonymous_inloop", func(b *testing.B) {
for n := 0; n < b.N; n++ {
pd2 := struct {
Property1 int `json:"property1"`
Property2 int `json:"property2"`
Property3 int `json:"property3"`
Property4 float64 `json:"property4"`
Property5 float64 `json:"property5"`
Property6 bool `json:"property6"`
}{}
_ = json.Unmarshal(data, &pd2)
}
})
b.Run("inline_struct_anonymous_outloop", func(b *testing.B) {
pd2 := struct {
Property1 int `json:"property1"`
Property2 int `json:"property2"`
Property3 int `json:"property3"`
Property4 float64 `json:"property4"`
Property5 float64 `json:"property5"`
Property6 bool `json:"property6"`
}{}
for n := 0; n < b.N; n++ {
_ = json.Unmarshal(data, &pd2)
}
})
}
@c1982
Copy link
Author

c1982 commented Dec 25, 2020

output:

Running tool: /usr/local/go/bin/go test -benchmem -run=^$ -bench ^(Benchmark_Struct)$ console -v

goos: darwin
goarch: amd64
pkg: console
Benchmark_Struct
Benchmark_Struct/inline_struct
Benchmark_Struct/inline_struct-16         	  726765	      1673 ns/op	     416 B/op	       7 allocs/op
Benchmark_Struct/outline_struct_inloop
Benchmark_Struct/outline_struct_inloop-16 	  750530	      1649 ns/op	     416 B/op	       7 allocs/op
Benchmark_Struct/outline_struct_outloop
Benchmark_Struct/outline_struct_outloop-16         	  807182	      1638 ns/op	     360 B/op	       5 allocs/op
Benchmark_Struct/inline_struct_anoymous_inloop
Benchmark_Struct/inline_struct_anoymous_inloop-16  	  812196	      1550 ns/op	     408 B/op	       6 allocs/op
Benchmark_Struct/inline_struct_anoymous_outloop
Benchmark_Struct/inline_struct_anoymous_outloop-16 	  846127	      1602 ns/op	     360 B/op	       5 allocs/op
PASS
ok  	console	7.807s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment