Created
June 12, 2018 07:25
-
-
Save manofstick/6d80edf625b946149700906683bf365e to your computer and use it in GitHub Desktop.
Blow that stack...
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 Program | |
| open Perf | |
| open System | |
| open System.Diagnostics | |
| type ICreator<'T> = | |
| abstract Root : 'T | |
| abstract Next : 'T -> 'T | |
| type SimpleRecordChain = { | |
| Next : SimpleRecordChain | |
| } | |
| type SimpleUnionChain = | |
| | NoSimpleUnionChain | |
| | NextSimpleUnionChain of SimpleUnionChain | |
| type OptionRecordChain = { | |
| Next : option<OptionRecordChain> | |
| } | |
| type A = { | |
| Next : option<B> | |
| } | |
| and B = { | |
| Next : A | |
| } | |
| type Pair<'T,'U> = Pair of 'T * 'U | |
| type PairList<'T> = Nil | Cons of Pair<'T, PairList<'T>> | |
| let creatorSimpleRecordChain = { | |
| new ICreator<SimpleRecordChain> with | |
| member __.Root = Unchecked.defaultof<_> | |
| member __.Next current = { Next = current } | |
| } | |
| let creatorSimpleUnionChain = { | |
| new ICreator<SimpleUnionChain> with | |
| member __.Root = NoSimpleUnionChain | |
| member __.Next current = NextSimpleUnionChain current | |
| } | |
| let creatorGenericRecordChain = { | |
| new ICreator<OptionRecordChain> with | |
| member __.Root = { Next = None } | |
| member __.Next current = { Next = Some current } | |
| } | |
| let creatorABChain = { | |
| new ICreator<A> with | |
| member __.Root = { Next = None } | |
| member __.Next current = { Next = Some { Next = current } } | |
| } | |
| let creatorPairList = { | |
| new ICreator<PairList<int>> with | |
| member __.Root = Nil | |
| member __.Next current = Cons (Pair (42, current)) | |
| } | |
| let createLots (creator:ICreator<'T>) = | |
| let rec create x n = | |
| if n = 0 | |
| then x | |
| else create (creator.Next x) (n-1) | |
| create creator.Root 10000000 | |
| let test (creator:ICreator<'T>) = | |
| printf "\nTesting: %A; " typeof<'T> | |
| printf "Creating... " | |
| let a = createLots creator | |
| let b = createLots creator | |
| printf "Comparing... " | |
| let sw = Stopwatch.StartNew () | |
| let mutable check = true | |
| for i = 1 to 10 do | |
| check <- a.Equals b | |
| if not check then | |
| failwith "bad" | |
| printfn "Good => %dms" sw.ElapsedMilliseconds | |
| [<EntryPoint>] | |
| let main _ = | |
| printfn "%s (Is64BitProcess=%b)" Id.Name Environment.Is64BitProcess | |
| test creatorPairList | |
| test creatorSimpleRecordChain | |
| test creatorSimpleUnionChain | |
| test creatorABChain | |
| test creatorGenericRecordChain | |
| 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment