Skip to content

Instantly share code, notes, and snippets.

@Szer
Last active April 7, 2020 18:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Szer/9f20f116dbc6ca2397af152ec673afba to your computer and use it in GitHub Desktop.
Save Szer/9f20f116dbc6ca2397af152ec673afba to your computer and use it in GitHub Desktop.
tryPick benches

Summary

BenchmarkDotNet=v0.12.0, OS=macOS 10.15.3 (19D76) [Darwin 19.3.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-FLMOIG : .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 l Mean Error StdDev Gen 0 Gen 1 Gen 2 Allocated
List [1; 2; 3; ... ] 196.0 ns 8.33 ns 7.80 ns - - - 24 B
List [1; 2; 3; ... ] 18,602.3 ns 500.69 ns 468.35 ns - - - 24 B
List [1; 2; 3; ... ] 2,972,947.4 ns 158,609.99 ns 148,363.88 ns - - - 24 B
Seq [1; 2; 3; ... ] 881.0 ns 24.95 ns 23.34 ns - - - 64 B
Seq [1; 2; 3; ... ] 83,529.1 ns 3,933.55 ns 3,679.44 ns - - - 64 B
Seq [1; 2; 3; ... ] 8,676,927.6 ns 403,418.84 ns 377,358.24 ns - - - 75 B
Linq [1; 2; 3; ... ] 17,153.7 ns 530.12 ns 495.88 ns - - - 664 B
Linq [1; 2; 3; ... ] 99,450.7 ns 2,185.90 ns 2,044.69 ns - - - 664 B
Linq [1; 2; 3; ... ] 8,562,087.4 ns 314,010.99 ns 293,726.07 ns - - - 674 B
Manual [1; 2; 3; ... ] 369.0 ns 14.82 ns 13.86 ns - - - -
Manual [1; 2; 3; ... ] 36,235.6 ns 643.99 ns 602.39 ns - - - -
Manual [1; 2; 3; ... ] 4,670,922.6 ns 97,809.28 ns 91,490.86 ns - - - 33 B
module Bench
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Running
open System.Linq
[<SimpleJob(launchCount = 3, warmupCount = 3, targetCount = 5)>]
[<GcServer(true)>]
[<MemoryDiagnoser>]
[<MarkdownExporterAttribute.GitHub>]
type Benchmarks () =
member __.Data() = seq {
[1..100]
[1..10000]
[1..1000000]
}
// [<CompiledName("TryPick")>]
// let rec tryPick chooser list =
// match list with
// | [] -> None
// | h :: t ->
// match chooser h with
// | None -> tryPick chooser t
// | r -> r
[<Benchmark>]
[<ArgumentsSource("Data")>]
member this.List (l: _ list) = List.tryPick (fun x -> if x = -1 then Some x else None) l
// [<CompiledName("TryPick")>]
// let tryPick chooser (source : seq<'T>) =
// checkNonNull "source" source
// use e = source.GetEnumerator()
// let mutable res = None
// while (Option.isNone res && e.MoveNext()) do
// res <- chooser e.Current
// res
[<Benchmark>]
[<ArgumentsSource("Data")>]
member this.Seq (l: _ list) = Seq.tryPick (fun x -> if x = -1 then Some x else None) l
[<Benchmark>]
[<ArgumentsSource("Data")>]
member this.Linq (l: _ list) =
try
Some(l.First(fun x -> x = -1))
with _ -> None
[<Benchmark>]
[<ArgumentsSource("Data")>]
member this.Manual (l: _ list) =
let mutable result = ValueNone
let mutable list = l
while not list.IsEmpty && result.IsNone do
if list.Head = -1 then
result <- ValueSome list.Head
else
list <- list.Tail
if result.IsNone then None
else Some result.Value
[<EntryPoint>]
let main _ =
let _ = BenchmarkRunner.Run<Benchmarks>()
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment