Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save daniellowtw/8d22dd19e5ac70c9659d08a0b76c91d2 to your computer and use it in GitHub Desktop.
Save daniellowtw/8d22dd19e5ac70c9659d08a0b76c91d2 to your computer and use it in GitHub Desktop.
benchmarking
package main
import (
"reflect"
"testing"
"github.com/aws/aws-sdk-go/aws/request"
)
func BenchmarkFooF(b *testing.B) {
for i := 0; i < b.N; i++ {
f()
}
}
func BenchmarkFooG(b *testing.B) {
for i := 0; i < b.N; i++ {
g()
}
}
func f() {
r := &request.Request{
Params: &struct{}{},
}
if !reflect.ValueOf(r.Params).IsNil() { // verifies that r.Params is not nil
_ = reflect.ValueOf(r.Params).Elem()
}
}
func g() {
r := &request.Request{
Params: &struct{}{},
}
of := reflect.ValueOf(r.Params)
if !of.IsNil() { // verifies that r.Params is not nil
_ = of.Elem()
}
}
@daniellowtw
Copy link
Author

go test -benchmem -bench=. ./...
goos: darwin
goarch: amd64

cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
BenchmarkFooF-16 45211039 24.32 ns/op 0 B/op 0 allocs/op
BenchmarkFooG-16 203895636 6.264 ns/op 0 B/op 0 allocs/op
PASS
ok 3.791s

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