Last active
March 18, 2025 05:01
-
-
Save dnagir/7b11332493150267c20719e4624d75f9 to your computer and use it in GitHub Desktop.
By value vs by reference
This file contains hidden or 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 ( | |
"testing" | |
) | |
func byValue(d Data) Data { | |
return d | |
} | |
func byPointer(d *Data) *Data { | |
return d | |
} | |
func BenchmarkByValue(b *testing.B) { | |
var data Data | |
for i := 0; i < b.N; i++ { | |
data = byValue(data) | |
} | |
} | |
func BenchmarkByPointer(b *testing.B) { | |
data := new(Data) | |
for i := 0; i < b.N; i++ { | |
data = byPointer(data) | |
} | |
} |
This file contains hidden or 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
module example.com | |
go 1.24.1 |
This file contains hidden or 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 ( | |
"fmt" | |
"unsafe" | |
) | |
type Data struct { | |
V1 float64 | |
V2 float64 | |
V3 float64 | |
V4 float64 | |
V5 float64 | |
V6 float64 | |
V7 float64 | |
V8 float64 | |
V9 float64 | |
V10 float64 | |
V11 float64 | |
V12 float64 | |
V13 float64 | |
V14 float64 | |
V15 float64 | |
V16 float64 | |
V17 float64 | |
V18 float64 | |
V19 float64 | |
V20 float64 | |
V21 float64 | |
V22 float64 | |
V23 float64 | |
V24 float64 | |
V25 float64 | |
V26 float64 | |
V27 float64 | |
V28 float64 | |
V29 float64 | |
V30 float64 | |
V31 float64 | |
V32 float64 | |
V33 float64 | |
V34 float64 | |
V35 float64 | |
V36 float64 | |
V37 float64 | |
V38 float64 | |
V39 float64 | |
V40 float64 | |
V41 float64 | |
V42 float64 | |
V43 float64 | |
V44 float64 | |
V45 float64 | |
V46 float64 | |
V47 float64 | |
V48 float64 | |
V49 float64 | |
V50 float64 | |
} | |
func main() { | |
fmt.Println("Size of the structure: ", unsafe.Sizeof(Data{}), "bytes") | |
} |
This file contains hidden or 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
$ go run . | |
Size of the structure: 400 bytes | |
$ go test -bench=. -benchtime=10s | |
goos: darwin | |
goarch: arm64 | |
pkg: example.com | |
cpu: Apple M1 Pro | |
BenchmarkByValue-8 742338524 16.10 ns/op | |
BenchmarkByPointer-8 1000000000 0.3127 ns/op | |
PASS | |
ok example.com 14.146s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment