Skip to content

Instantly share code, notes, and snippets.

@nicerobot

nicerobot/Makefile

Forked from teivah/pointer_test.go
Last active Feb 22, 2021
Embed
What would you like to do?
Notice that the issue is with returning a pointer, not with passing a pointer. Passing the pointer is indeed faster than passing the value, as expected. This discrepancy is likely the effect of [having to escape the pointer's data to the heap](https://www.ardanlabs.com/blog/2017/05/language-mechanics-on-escape-analysis.html).
goos: darwin
goarch: amd64
pkg: gist.github.com/nicerobot/cdd5ff0d660325a29d6aa2fd2c4f4d03
BenchmarkInPointerOutPointer-12 13650507 78.2 ns/op
BenchmarkInPointerOutValue-12 52139656 21.7 ns/op
BenchmarkInValueOutValue-12 44229886 27.8 ns/op
BenchmarkInValueOutPointer-12 13691456 85.0 ns/op
PASS
ok gist.github.com/nicerobot/cdd5ff0d660325a29d6aa2fd2c4f4d03 5.056s
bench:
go test -bench=. 2>&1 | tee bench.out
package main
import (
"encoding/json"
"testing"
)
type foo struct {
ID string `json:"_id"`
Index int `json:"index"`
GUID string `json:"guid"`
IsActive bool `json:"isActive"`
Balance string `json:"balance"`
Picture string `json:"picture"`
Age int `json:"age"`
EyeColor string `json:"eyeColor"`
Name string `json:"name"`
Gender string `json:"gender"`
Company string `json:"company"`
Email string `json:"email"`
Phone string `json:"phone"`
Address string `json:"address"`
About string `json:"about"`
Registered string `json:"registered"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Greeting string `json:"greeting"`
FavoriteFruit string `json:"favoriteFruit"`
}
type bar struct {
ID string
Index int
GUID string
IsActive bool
Balance string
Picture string
Age int
EyeColor string
Name string
Gender string
Company string
Email string
Phone string
Address string
About string
Registered string
Latitude float64
Longitude float64
Greeting string
FavoriteFruit string
}
var input foo
func init() {
err := json.Unmarshal([]byte(`{
"_id": "5d2f4fcf76c35513af00d47e",
"index": 1,
"guid": "ed687a14-590b-4d81-b0cb-ddaa857874ee",
"isActive": true,
"balance": "$3,837.19",
"picture": "http://placehold.it/32x32",
"age": 28,
"eyeColor": "green",
"name": "Rochelle Espinoza",
"gender": "female",
"company": "PARLEYNET",
"email": "rochelleespinoza@parleynet.com",
"phone": "+1 (969) 445-3766",
"address": "956 Little Street, Jugtown, District Of Columbia, 6396",
"about": "Excepteur exercitation labore ut cupidatat laboris mollit ad qui minim aliquip nostrud anim adipisicing est. Nisi sunt duis occaecat aliquip est irure Lorem irure nulla tempor sit sunt. Eiusmod laboris ex est velit minim ut cillum sunt laborum labore ad sunt.\r\n",
"registered": "2016-03-20T12:07:25 -00:00",
"latitude": 61.471517,
"longitude": 54.01596,
"greeting": "Hello, Rochelle Espinoza!You have 9 unread messages.",
"favoriteFruit": "banana"
}`), &input)
if err != nil {
panic(err)
}
}
func inPointerOutValue(in *foo) bar {
return bar{
ID: in.ID,
Address: in.Address,
Email: in.Email,
Index: in.Index,
Name: in.Name,
About: in.About,
Age: in.Age,
Balance: in.Balance,
Company: in.Company,
EyeColor: in.EyeColor,
FavoriteFruit: in.FavoriteFruit,
Gender: in.Gender,
Greeting: in.Greeting,
GUID: in.GUID,
IsActive: in.IsActive,
Latitude: in.Latitude,
Longitude: in.Longitude,
Phone: in.Phone,
Picture: in.Picture,
Registered: in.Registered,
}
}
func inPointerOutPointer(in *foo) *bar {
return &bar{
ID: in.ID,
Address: in.Address,
Email: in.Email,
Index: in.Index,
Name: in.Name,
About: in.About,
Age: in.Age,
Balance: in.Balance,
Company: in.Company,
EyeColor: in.EyeColor,
FavoriteFruit: in.FavoriteFruit,
Gender: in.Gender,
Greeting: in.Greeting,
GUID: in.GUID,
IsActive: in.IsActive,
Latitude: in.Latitude,
Longitude: in.Longitude,
Phone: in.Phone,
Picture: in.Picture,
Registered: in.Registered,
}
}
func inValueOutValue(in foo) bar {
return bar{
ID: in.ID,
Address: in.Address,
Email: in.Email,
Index: in.Index,
Name: in.Name,
About: in.About,
Age: in.Age,
Balance: in.Balance,
Company: in.Company,
EyeColor: in.EyeColor,
FavoriteFruit: in.FavoriteFruit,
Gender: in.Gender,
Greeting: in.Greeting,
GUID: in.GUID,
IsActive: in.IsActive,
Latitude: in.Latitude,
Longitude: in.Longitude,
Phone: in.Phone,
Picture: in.Picture,
Registered: in.Registered,
}
}
func inValueOutPointer(in foo) *bar {
return &bar{
ID: in.ID,
Address: in.Address,
Email: in.Email,
Index: in.Index,
Name: in.Name,
About: in.About,
Age: in.Age,
Balance: in.Balance,
Company: in.Company,
EyeColor: in.EyeColor,
FavoriteFruit: in.FavoriteFruit,
Gender: in.Gender,
Greeting: in.Greeting,
GUID: in.GUID,
IsActive: in.IsActive,
Latitude: in.Latitude,
Longitude: in.Longitude,
Phone: in.Phone,
Picture: in.Picture,
Registered: in.Registered,
}
}
var pointerResult *bar
func BenchmarkInPointerOutPointer(b *testing.B) {
var r *bar
b.ResetTimer()
for i := 0; i < b.N; i++ {
r = inPointerOutPointer(&input)
}
pointerResult = r
}
func BenchmarkInPointerOutValue(b *testing.B) {
var r bar
b.ResetTimer()
for i := 0; i < b.N; i++ {
r = inPointerOutValue(&input)
}
valueResult = r
}
var valueResult bar
func BenchmarkInValueOutValue(b *testing.B) {
var r bar
b.ResetTimer()
for i := 0; i < b.N; i++ {
r = inValueOutValue(input)
}
valueResult = r
}
func BenchmarkInValueOutPointer(b *testing.B) {
var r *bar
b.ResetTimer()
for i := 0; i < b.N; i++ {
r = inValueOutPointer(input)
}
pointerResult = r
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment