Created
June 8, 2022 15:20
-
-
Save daniellowtw/8d22dd19e5ac70c9659d08a0b76c91d2 to your computer and use it in GitHub Desktop.
benchmarking
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 ( | |
"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() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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