Skip to content

Instantly share code, notes, and snippets.

@Lanayx
Last active April 7, 2020 17:56
Show Gist options
  • Save Lanayx/cad03a7dde653bc47897fc599913776a to your computer and use it in GitHub Desktop.
Save Lanayx/cad03a7dde653bc47897fc599913776a to your computer and use it in GitHub Desktop.
Records vs Interfaces speed
namespace Benchmarks
open BenchmarkDotNet.Attributes
open System.IO
open System.Runtime.CompilerServices
[<Interface>]
type ILogger =
abstract Info: string -> unit
type LogClass() =
member this.Info0 (i: string) =
TextWriter.Null.WriteLine(i)
TextWriter.Null.WriteLine(i)
TextWriter.Null.WriteLine(i)
TextWriter.Null.WriteLine(i)
TextWriter.Null.WriteLine(i)
interface ILogger with
member this.Info i =
TextWriter.Null.WriteLine(i)
TextWriter.Null.WriteLine(i)
TextWriter.Null.WriteLine(i)
TextWriter.Null.WriteLine(i)
TextWriter.Null.WriteLine(i)
module LogRec2 =
let log2 (i: string) =
TextWriter.Null.WriteLine(i)
TextWriter.Null.WriteLine(i)
TextWriter.Null.WriteLine(i)
TextWriter.Null.WriteLine(i)
TextWriter.Null.WriteLine(i)
type LogRec =
{
Info: string -> unit
}
//[<MemoryDiagnoser>]
type RecordVsLambda() =
let text = "abcd"
let classExample = LogClass()
let interfaceExample = LogClass() :> ILogger
let recordExample =
{ Info = fun i ->
TextWriter.Null.WriteLine(i)
TextWriter.Null.WriteLine(i)
TextWriter.Null.WriteLine(i)
TextWriter.Null.WriteLine(i)
TextWriter.Null.WriteLine(i) }
let recordExample2 =
{ Info = LogRec2.log2 }
[<Benchmark(Baseline=true)>]
member _.InterfaceExample() =
interfaceExample.Info text
interfaceExample.Info text
interfaceExample.Info text
[<Benchmark>]
member _.RecordExample() =
recordExample.Info text
recordExample.Info text
recordExample.Info text
[<Benchmark>]
member _.RecordExample2() =
recordExample2.Info text
recordExample2.Info text
recordExample2.Info text
[<Benchmark>]
member _.ClassExample() =
classExample.Info0 text
classExample.Info0 text
classExample.Info0 text
[<Benchmark>]
member _.ByHand() =
TextWriter.Null.WriteLine(text)
TextWriter.Null.WriteLine(text)
TextWriter.Null.WriteLine(text)
TextWriter.Null.WriteLine(text)
TextWriter.Null.WriteLine(text)
TextWriter.Null.WriteLine(text)
TextWriter.Null.WriteLine(text)
TextWriter.Null.WriteLine(text)
TextWriter.Null.WriteLine(text)
TextWriter.Null.WriteLine(text)
TextWriter.Null.WriteLine(text)
TextWriter.Null.WriteLine(text)
TextWriter.Null.WriteLine(text)
TextWriter.Null.WriteLine(text)
TextWriter.Null.WriteLine(text)
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 |
|----------------- |----------:|----------:|----------:|------:|--------:|
| InterfaceExample | 6.6501 ns | 0.0731 ns | 0.0648 ns | 1.00 | 0.00 |
| RecordExample | 3.5110 ns | 0.0595 ns | 0.0556 ns | 0.53 | 0.01 |
| RecordExample2 | 8.3540 ns | 0.1371 ns | 0.1283 ns | 1.25 | 0.02 |
| ClassExample | 5.6655 ns | 0.0624 ns | 0.0584 ns | 0.85 | 0.01 |
| ByHand | 0.7271 ns | 0.0115 ns | 0.0107 ns | 0.11 | 0.00 |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment