Skip to content

Instantly share code, notes, and snippets.

@liboz
Created July 28, 2016 02:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liboz/718a6477efcc9a9550c24eb8307a9aed to your computer and use it in GitHub Desktop.
Save liboz/718a6477efcc9a9550c24eb8307a9aed to your computer and use it in GitHub Desktop.
open BenchmarkDotNet.Running
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Configs
open BenchmarkDotNet.Diagnostics.Windows
let scanOriginal<'T,'State> f (s:'State) (list:'T list) =
let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(f)
let rec loop s xs acc =
match xs with
| [] -> List.rev acc
| (h::t) -> let s = f.Invoke(s,h) in loop s t (s :: acc)
loop s list [s]
let rec mapi2aux n (f:OptimizedClosures.FSharpFunc<_,_,_,_>) list1 list2 acc =
match list1,list2 with
| [],[] -> List.rev acc
| (h1::t1), (h2::t2) -> let x = f.Invoke(n,h1,h2) in mapi2aux (n+1) f t1 t2 (x :: acc)
| _ -> invalidArg "list2" "listsHadDifferentLengths"
let mapi2Original f list1 list2 =
let f = OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt(f)
mapi2aux 0 f list1 list2 []
let rec map3aux (f:OptimizedClosures.FSharpFunc<_,_,_,_>) list1 list2 list3 acc =
match list1,list2,list3 with
| [],[],[] -> List.rev acc
| (h1::t1), (h2::t2),(h3::t3) -> let x = f.Invoke(h1,h2,h3) in map3aux f t1 t2 t3 (x :: acc)
| _ -> invalidArg "list3" "listsHadDifferentLengths"
let map3Original f list1 list2 list3 =
let f = OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt(f)
map3aux f list1 list2 list3 []
open System
type map3 () =
[<Params(10L, 100L, 10000L, 1000000L)>]
member val public count = 0L with get, set
[<Benchmark>]
member this.map3 () =
List.map3 (fun x y z -> (x + y + z)) [1L..this.count] [1L..this.count] [1L..this.count]
[<Benchmark>]
member this.map3Original () =
map3Original (fun x y z -> (x + y + z) ) [1L..this.count] [1L..this.count] [1L..this.count]
type mapi2 () =
[<Params(10L, 100L, 10000L, 1000000L)>]
member val public count = 0L with get, set
[<Benchmark>]
member this.mapi2 () =
List.mapi2 (fun i x y -> (x + y)*Convert.ToInt64(i)) [1L..this.count] [1L..this.count]
[<Benchmark>]
member this.mapi2Original () =
mapi2Original (fun i x y -> (x + y)*Convert.ToInt64(i)) [1L..this.count] [1L..this.count]
type scan () =
[<Params(10L, 100L, 10000L, 1000000L)>]
member val public count = 0L with get, set
[<Benchmark>]
member this.scan () =
List.scan (fun acc i -> acc + i) 0L [1L..this.count]
[<Benchmark>]
member this.map3Original () =
scanOriginal (fun acc i -> acc + i) 0L [1L..this.count]
let [<EntryPoint>] main args =
let config = ManualConfig.Create(DefaultConfig.Instance)
config.Add(new MemoryDiagnoser())
config.Add(new InliningDiagnoser())
BenchmarkRunner.Run<map3>(config) |> ignore
BenchmarkRunner.Run<mapi2>(config) |> ignore
BenchmarkRunner.Run<scan>(config) |> ignore
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment