Skip to content

Instantly share code, notes, and snippets.

@Horusiath
Created March 26, 2020 11:29
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 Horusiath/03357b136043464cbb59fb09f5ec2f4f to your computer and use it in GitHub Desktop.
Save Horusiath/03357b136043464cbb59fb09f5ec2f4f to your computer and use it in GitHub Desktop.
BenchmarkDotNet=v0.12.0, OS=Windows 10.0.18363
Intel Core i5-4430 CPU 3.00GHz (Haswell), 1 CPU, 4 logical and 4 physical cores
.NET Core SDK=3.1.200
  [Host]     : .NET Core 3.1.2 (CoreCLR 4.700.20.6602, CoreFX 4.700.20.6702), X64 RyuJIT DEBUG
  DefaultJob : .NET Core 3.1.2 (CoreCLR 4.700.20.6602, CoreFX 4.700.20.6702), X64 RyuJIT

Method Mean Error StdDev Ratio Gen 0 Gen 1 Gen 2 Allocated
ConsoleWriteLine 3.263 ns 0.0060 ns 0.0053 ns 1.00 - - - -
LogInfo 1.943 ns 0.0273 ns 0.0255 ns 0.59 - - - -
module DemoFs.Benchmarks
open BenchmarkDotNet.Attributes
open System.IO
open System.Runtime.CompilerServices
[<Interface>]
type ILogger =
abstract Info: string -> unit
[<Interface>]
type ILog =
abstract Logger: ILogger
[<Sealed>]
type LiveLogger() =
[<MethodImpl(MethodImplOptions.AggressiveInlining)>]
member _.Info (fmt: string) = TextWriter.Null.WriteLine(fmt)
interface ILogger with member this.Info fmt = this.Info fmt
module Log =
let live = LiveLogger()
[<MethodImpl(MethodImplOptions.AggressiveInlining)>]
let info (env: #ILog) msg = env.Logger.Info msg
[<Struct;NoEquality;NoComparison>]
type AppEnv =
member inline _.Logger : ILogger = upcast Log.live
interface ILog with
member this.Logger = this.Logger
[<MemoryDiagnoser>]
type EnvBenchmark() =
static let message = "hello world"
[<Benchmark(Baseline=true)>]
member _.ConsoleWriteLine() = TextWriter.Null.WriteLine(message)
[<Benchmark>]
member _.LogInfo() = Log.info (AppEnv()) message
open System
open System.Reflection
open System.Threading
open BenchmarkDotNet.Running
[<EntryPoint>]
let main argv =
BenchmarkSwitcher.FromAssembly(Assembly.GetExecutingAssembly()).Run(argv) |> ignore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment