Last active
December 27, 2020 14:53
-
-
Save c1982/2b36f310c6172c9b74f37da63ce670a8 to your computer and use it in GitHub Desktop.
Benchmarks for inline, outline, anonymous struct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output: