Created
April 3, 2017 22:52
-
-
Save jackmott/db3479bbd8a17ad8578bece7aec163cb to your computer and use it in GitHub Desktop.
map benchmark
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
| open System.Collections.Concurrent | |
| open System.Threading.Tasks | |
| open BenchmarkDotNet.Attributes | |
| open BenchmarkDotNet.Configs | |
| open BenchmarkDotNet.Running | |
| open BenchmarkDotNet.Jobs | |
| open System | |
| // Learn more about F# at http://fsharp.org | |
| // See the 'F# Tutorial' project for more help. | |
| let mapNew (f: 'T -> 'U) (array : 'T[]) : 'U[]= | |
| let inputLength = array.Length | |
| let result = Array.zeroCreate inputLength | |
| let rangePartition = Partitioner.Create(0,inputLength) | |
| Parallel.ForEach(rangePartition, | |
| fun (start,finish) _ -> | |
| for i in start .. finish - 1 do | |
| result.[i] <- f array.[i]) |> ignore | |
| result | |
| let mapOld (f: 'T -> 'U) (array : 'T[]) : 'U[]= | |
| let inputLength = array.Length | |
| let result = Array.zeroCreate inputLength | |
| Parallel.For(0, inputLength, fun i -> | |
| result.[i] <- f array.[i]) |> ignore | |
| result | |
| [<MemoryDiagnoser>] | |
| type ParallelBench () = | |
| let mutable array = [||] | |
| let mutable intArray = [||] | |
| [<Params (1000,10000,50000,100000,1000000)>] | |
| member val public Length = 0 with get,set | |
| [<Setup>] | |
| member self.SetupData() = | |
| array <- Array.create self.Length 2.0 | |
| intArray <- Array.create self.Length 2 | |
| [<Benchmark>] | |
| member self.MapOldDistanceFloats() = | |
| array |> mapOld (fun x -> System.Math.Sqrt(x*x+x*x)) | |
| [<Benchmark>] | |
| member self.MapNewDistanceFloats() = | |
| array |> mapNew (fun x -> System.Math.Sqrt(x*x+x*x)) | |
| [<Benchmark>] | |
| member self.MapOldPlus1Ints() = | |
| intArray |> mapOld (fun x -> x+1) | |
| [<Benchmark>] | |
| member self.MapNewPlus1Ints() = | |
| intArray |> mapNew (fun x -> x+1) | |
| [<EntryPoint>] | |
| let main argv = | |
| BenchmarkRunner.Run<ParallelBench>(ManualConfig.Create(DefaultConfig.Instance).With(Job.RyuJitX64)) |> ignore | |
| 0 // return an integer exit code | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment