Skip to content

Instantly share code, notes, and snippets.

@Lanayx
Created March 26, 2020 21:01
Show Gist options
  • Save Lanayx/95d7b49d952b2bf6fa0629093e22cfb6 to your computer and use it in GitHub Desktop.
Save Lanayx/95d7b49d952b2bf6fa0629093e22cfb6 to your computer and use it in GitHub Desktop.
open BenchmarkDotNet.Attributes
open System.IO
open System.Runtime.CompilerServices
[<Interface>]
type ILog =
abstract Info: string -> unit
module Log =
[<MethodImpl(MethodImplOptions.NoInlining)>]
let info (env: #ILog) fmt =
env.Info fmt
env.Info fmt
env.Info fmt
env.Info fmt
env.Info fmt
env.Info fmt
env.Info fmt
[<Struct;NoEquality;NoComparison>]
type AppEnv =
interface ILog with
member this.Info msg = TextWriter.Null.WriteLine(msg)
[<MemoryDiagnoser>]
type EnvBenchmark() =
static let message = "hello world"
[<Benchmark(Baseline=true)>]
member _.ConsoleWriteLine() =
TextWriter.Null.WriteLine(message)
TextWriter.Null.WriteLine(message)
TextWriter.Null.WriteLine(message)
TextWriter.Null.WriteLine(message)
TextWriter.Null.WriteLine(message)
TextWriter.Null.WriteLine(message)
TextWriter.Null.WriteLine(message)
[<Benchmark>]
member _.LogInfo() = Log.info (AppEnv()) message
BenchmarkDotNet=v0.12.0, OS=Windows 10.0.18363
AMD Ryzen 7 2700X, 1 CPU, 16 logical and 8 physical cores
.NET Core SDK=3.1.101
[Host] : .NET Core 3.1.1 (CoreCLR 4.700.19.60701, CoreFX 4.700.19.60801), X64 RyuJIT DEBUG
DefaultJob : .NET Core 3.1.1 (CoreCLR 4.700.19.60701, CoreFX 4.700.19.60801), X64 RyuJIT
| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
|----------------- |---------:|---------:|---------:|------:|--------:|------:|------:|------:|----------:|
| ConsoleWriteLine | 10.18 ns | 0.279 ns | 0.342 ns | 1.00 | 0.00 | - | - | - | - |
| LogInfo | 15.18 ns | 0.377 ns | 0.619 ns | 1.50 | 0.10 | - | - | - | - |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment