-
Notifications
You must be signed in to change notification settings - Fork 104
Make left zero dynamic (rewrite) #503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
+79
−8
Conversation
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
Closed
Member
Author
|
perf script: #r "src/FSharpPlus/bin/Release/netstandard2.0/FSharpPlus.dll"
// #r "nuget:FSharpPlus"
open System
open FSharpPlus
// try `traverse`
// let xs = traverse Some [1;2;3]
// let ys = traverse List.singleton [1;2;3]
// type without IsLeftZero or Empty
type Wrapped<'a> = Wrapped of 'a with
static member Return (x: 'a) = Wrapped x
static member (>>=) (Wrapped a, f) = f a
// type with Empty
type Maybe<'a> = Just of 'a | Nothing with
static member Empty : Maybe<'a> = Nothing
// type with IsLeftZero
type Either<'a, 'b> = Left of 'a | Right of 'b with
static member IsLeftZero(x: Either<'a, 'b>) =
match x with Right _ -> true | _ -> false
// struct type without IsLeftZero or Empty
type [<Struct>] SWrapped<'a> = SWrapped of 'a with
static member Return (x: 'a) = SWrapped x
static member (>>=) (SWrapped a, f) = f a
// struct type with Empty
type [<Struct>] SMaybe<'a> = SJust of 'a | SNothing with
static member Empty : SMaybe<'a> = SNothing
// struct type with IsLeftZero
type [<Struct>] SEither<'a, 'b> = SLeft of l:'a | SRight of r:'b with
static member IsLeftZero(x: SEither<'a, 'b>) =
match x with SRight _ -> true | _ -> false
// perf
open FSharpPlus.Control
let [<Literal>] N = 10000000
let before = DateTime.Now
for _ = 1 to N do
// built-in types
assert not (IsLeftZero.Invoke(Some 42))
assert IsLeftZero.Invoke(None : int option)
assert not (IsLeftZero.Invoke([4; 2]))
assert IsLeftZero.Invoke([] : int list)
assert not (IsLeftZero.Invoke([|"4"; "2"|]))
assert IsLeftZero.Invoke([||] : string array)
assert not (IsLeftZero.Invoke(Seq.singleton 42))
assert IsLeftZero.Invoke(Seq.empty : int seq)
assert not (IsLeftZero.Invoke(Ok 42 : Result<int, string>))
assert IsLeftZero.Invoke(Error "msg" : Result<int, string>)
assert not (IsLeftZero.Invoke(Choice1Of2 42 : Choice<int, string>))
assert IsLeftZero.Invoke(Choice2Of2 "msg" : Choice<int, string>)
// user-defined types
assert not (IsLeftZero.Invoke(Wrapped 42))
assert not (IsLeftZero.Invoke(Just 42))
assert IsLeftZero.Invoke(Nothing : Maybe<int>)
assert not (IsLeftZero.Invoke(Left 42 : Either<int, string>))
assert IsLeftZero.Invoke(Right "msg" : Either<int, string>)
// user-defined struct types
assert not (IsLeftZero.Invoke(SWrapped 42))
assert not (IsLeftZero.Invoke(SJust 42))
assert IsLeftZero.Invoke(SNothing : SMaybe<int>)
assert not (IsLeftZero.Invoke(SLeft 42 : SEither<int, string>))
assert IsLeftZero.Invoke(SRight "msg" : SEither<int, string>)
let after = DateTime.Now
let total = (after - before).TotalMilliseconds
printfn "OK: %fms total, %fms avg" (total) (total / float N)The result on my PC: 9ms to 12ms of overhead regardless of:
UPDATE: tried adding struct types to see if boxing/unboxing would affect the performance, but it seems not (still constant ~10ms overhead). |
gusty
approved these changes
Sep 14, 2022
gusty
added a commit
that referenced
this pull request
Nov 14, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Related: #501