Skip to content

Instantly share code, notes, and snippets.

@teivah
Created July 17, 2019 16:56
Show Gist options
  • Save teivah/a32a8e9039314a48f03538f3f9535537 to your computer and use it in GitHub Desktop.
Save teivah/a32a8e9039314a48f03538f3f9535537 to your computer and use it in GitHub Desktop.
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 byPointer(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 byValue(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 BenchmarkByPointer(b *testing.B) {
var r *bar
b.ResetTimer()
for i := 0; i < b.N; i++ {
r = byPointer(&input)
}
pointerResult = r
}
var valueResult bar
func BenchmarkByValue(b *testing.B) {
var r bar
b.ResetTimer()
for i := 0; i < b.N; i++ {
r = byValue(input)
}
valueResult = r
}
@mrsndmn
Copy link

mrsndmn commented Jan 29, 2020

BenchmarkByPointer-2                    10000000               121 ns/op             288 B/op          1 allocs/op
BenchmarkByValue-2                      20000000                78.0 ns/op             0 B/op          0 allocs/op

@nicerobot
Copy link

Here's a more fair comparison.

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

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.

@ColaWsl
Copy link

ColaWsl commented Jul 3, 2024

Based WSL2 Ubuntu20..04
goos: linux
goarch: amd64
BenchmarkByPointer-8 13206825 98.49 ns/op
BenchmarkByValue-8 48335836 25.23 ns/op

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