Skip to content

Instantly share code, notes, and snippets.

@liboz
Created January 17, 2017 16:35
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/b17cc1740f5bdfdd0bc516df72b55081 to your computer and use it in GitHub Desktop.
Save liboz/b17cc1740f5bdfdd0bc516df72b55081 to your computer and use it in GitHub Desktop.
let mapFold (f:'a -> 'b -> 'c * 'a) (s:'a) (l:'b list) : 'c list * 'a =
// microbenchmark suggested this implementation is faster than the simpler recursive one, and this function is called a lot
let mutable s = s
let mutable r = []
for x in l do
let x',s' = f s x
s <- s'
r <- x' :: r
List.rev r, s
let mapFoldLibrary (f:'a -> 'b -> 'c * 'a) (s:'a) (l:'b list) : 'c list * 'a =
match l with
| [] -> [], s
| [h] -> let h',s' = f s h
[h'], s'
| _ ->
List.mapFold f s l
let g a b = b, (a + 1)
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Jobs
open BenchmarkDotNet.Configs
open BenchmarkDotNet.Diagnostics.Windows
open BenchmarkDotNet.Running
open BenchmarkDotNet.Environments
type mapfold () =
[<Params(0, 1, 2, 3, 4, 5, 10)>]
member val public count = 0 with get, set
member val public dummy = [] with get, set
[<Setup>]
member this.SetupData() =
this.dummy <- [ 1..this.count ]
[<Benchmark>]
member this.mapFoldLibrary () =
this.dummy
|> mapFoldLibrary g 0
[<Benchmark>]
member this.old () =
this.dummy
|> mapFold g 0
let makeJob (jit: Jit) (platform: Platform) =
let job = new Job()
job.Env.Jit <- jit
job.Env.Platform <- platform
//job.Run.LaunchCount <- 5
//job.Run.TargetCount <- 50
job
let [<EntryPoint>] main args =
let config = ManualConfig.Create(DefaultConfig.Instance)
config.Add(MemoryDiagnoser())
config.Add(makeJob Jit.RyuJit Platform.X64)
config.Add(makeJob Jit.LegacyJit Platform.X86)
BenchmarkRunner.Run<mapfold>(config) |> ignore
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment