Created
January 6, 2020 14:13
-
-
Save Szer/4e9205946bfb92e41ef657af246c83b4 to your computer and use it in GitHub Desktop.
seq benches 2
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 System.Linq | |
| open BenchmarkDotNet.Attributes | |
| open BenchmarkDotNet.Running | |
| [<SimpleJob(launchCount = 3, warmupCount = 3, targetCount = 5)>] | |
| [<GcServer(true)>] | |
| [<MemoryDiagnoser>] | |
| [<MarkdownExporterAttribute.GitHub>] | |
| type Benchs2() = | |
| let arr = [| 1 .. 1000000 |] | |
| let l = [ 1 .. 1000000 ] | |
| [<Benchmark>] | |
| member x.seqTest() = | |
| arr | |
| |> Seq.map int64 | |
| |> Seq.filter (fun v -> v % 2L = 0L) | |
| |> Seq.map ((+) 1L) | |
| |> Seq.sum | |
| [<Benchmark>] | |
| member x.arrayTest() = | |
| arr | |
| |> Array.map int64 | |
| |> Array.filter (fun v -> v % 2L = 0L) | |
| |> Array.map ((+) 1L) | |
| |> Array.sum | |
| [<Benchmark>] | |
| member x.linqTest() = arr.Select(int64).Where(fun v -> v % 2L = 0L).Select((+) 1L).Sum() | |
| [<Benchmark>] | |
| member x.listTest() = | |
| l | |
| |> List.map int64 | |
| |> List.filter (fun v -> v % 2L = 0L) | |
| |> List.map ((+) 1L) | |
| |> List.sum | |
| [<Benchmark(Baseline=true)>] | |
| member x.imperativeTest() = | |
| let mutable sum = 0L | |
| for i in arr do | |
| let i64 = int64 i | |
| if i64 % 2L = 0L then sum <- sum + i64 + 1L | |
| sum | |
| [<Benchmark>] | |
| member x.seqBuilderTest() = | |
| let mutable sum = 0L | |
| seq { | |
| for a in arr do | |
| let a64 = int64 a | |
| if a64 % 2L = 0L then sum <- sum + a64 + 1L | |
| } | |
| |> Seq.iter id | |
| sum | |
| [<Benchmark>] | |
| member x.seqBuilderWithSumTest() = | |
| seq { | |
| for a in arr do | |
| let a64 = int64 a | |
| if a64 % 2L = 0L then yield a64 | |
| } | |
| |> Seq.sum | |
| [<EntryPoint>] | |
| let main argv = | |
| let summary = BenchmarkRunner.Run<Benchs2>() | |
| 0 // return an integer exit code |
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
| // * Summary * | |
| BenchmarkDotNet=v0.12.0, OS=macOS Mojave 10.14.6 (18G95) [Darwin 18.7.0] | |
| Intel Core i9-9880H CPU 2.30GHz, 1 CPU, 16 logical and 8 physical cores | |
| .NET Core SDK=3.1.100 | |
| [Host] : .NET Core 3.1.0 (CoreCLR 4.700.19.56402, CoreFX 4.700.19.56404), X64 RyuJIT DEBUG | |
| Job-SSAMSJ : .NET Core 3.1.0 (CoreCLR 4.700.19.56402, CoreFX 4.700.19.56404), X64 RyuJIT | |
| Server=True IterationCount=5 LaunchCount=3 | |
| WarmupCount=3 | |
| | Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated | | |
| |---------------------- |------------:|----------:|----------:|------:|--------:|---------:|---------:|---------:|-----------:| | |
| | seqTest | 21,028.4 us | 570.81 us | 533.94 us | 27.45 | 1.03 | - | - | - | 506 B | | |
| | arrayTest | 10,169.4 us | 189.61 us | 177.36 us | 13.27 | 0.43 | 109.3750 | 109.3750 | 109.3750 | 16125885 B | | |
| | linqTest | 10,365.4 us | 239.31 us | 223.85 us | 13.54 | 0.63 | - | - | - | 500 B | | |
| | listTest | 29,674.5 us | 965.36 us | 902.99 us | 38.75 | 1.83 | 187.5000 | 93.7500 | - | 64000074 B | | |
| | imperativeTest | 766.7 us | 25.93 us | 24.26 us | 1.00 | 0.00 | - | - | - | - | | |
| | seqBuilderTest | 4,465.2 us | 184.04 us | 172.15 us | 5.83 | 0.28 | - | - | - | 224 B | | |
| | seqBuilderWithSumTest | 7,783.2 us | 68.96 us | 64.51 us | 10.16 | 0.34 | - | - | - | 171 B | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment