From db6275984f0919368b02a854ef63437ef599a71e Mon Sep 17 00:00:00 2001 From: SoulEmptyException <57583509+oissevalt@users.noreply.github.com> Date: Sun, 17 Mar 2024 15:07:32 +0800 Subject: [PATCH] Change benchmark examples --- README.md | 8 ++++++-- opzione_test.go | 33 +++++++++++++++------------------ 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index eabd36c..4c11b01 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,10 @@ # Opzione -_Opzione_ (Italian "option") is a Go library for optionals, with tracking of nested pointers. +_Opzione_ (Italian "option") is a Go library for optionals, with tracking of nested pointers. You can add it to your project using `go get`: + +```shell +go get github.com/oissevalt/opzione +``` The package provides two basic optional types (containers), `Simple` and `Chained`. @@ -59,7 +63,7 @@ func main() { } ``` -Note that the use of reflection can introduce additional operation time and memory usage, but best effort has been made to minimize such impact. According to benchmark (`opzione_test.go`), a sequence of operation on nested pointers took less than 400ns on average. +Note that the use of reflection can introduce additional operation time and memory usage, but best effort has been made to minimize such impact. According to benchmark (`opzione_test.go`), a sequence of operation on nested pointers took less than 300ns on average. For value types, `Chained` is expected to behave the same as `Simple`. diff --git a/opzione_test.go b/opzione_test.go index 6044cd4..dd5380c 100644 --- a/opzione_test.go +++ b/opzione_test.go @@ -6,30 +6,27 @@ import ( ) func BenchmarkOptionalReflection(b *testing.B) { - for i := 0; i < b.N; i++ { - value1 := 12 - value2 := 24 + number := 10 + pointer := &number - somePointer := ChainedSome[*int](&value1) - nonePointer := ChainedNone[*int]() + for i := 0; i < b.N; i++ { + optional := ChainedSome(&pointer) - value, err := somePointer.Value() - value, err = nonePointer.Value() - somePointer.Swap(&value2) - nonePointer.Swap(&value1) - value, err = nonePointer.Value() - value, err = somePointer.Take() + _ = optional.IsNone() + _, _ = optional.Value() - intptr := &value2 - nested := &intptr - ptrptrOption := ChainedSome[***int](&nested) + pointer = nil + _ = optional.IsNone() + _, _ = optional.Value() - valueptr, err := ptrptrOption.Value() + p2 := &number + _ = optional.Swap(&p2) - intptr = nil - valueptr, err = ptrptrOption.Value() + optional.With(func(i **int) { + _ = **i + 1 + }) - _, _, _ = value, err, valueptr + pointer = &number } }