Skip to content

Instantly share code, notes, and snippets.

@MichalBrylka
Last active April 20, 2021 13:19
Show Gist options
  • Save MichalBrylka/84887188e887b7cb13d53035766a6ad8 to your computer and use it in GitHub Desktop.
Save MichalBrylka/84887188e887b7cb13d53035766a6ad8 to your computer and use it in GitHub Desktop.
Cs vs Fs @ branching
namespace Fs
module Bench =
let condition x =
if (x = 1 || x = 2) then 1
elif(x = 3 || x = 4) then 2
else 0
let conditionReorder x =
if (x = 4 || x = 3) then 2
elif(x = 2 || x = 1) then 1
else 0
let conditionMatch x =
match x with
| 1 -> 1
| 2 -> 1
| 3 -> 2
| 4 -> 2
| _ -> 0
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace CsVsFs
{
class Program
{
static void Main(string[] args)
{
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
}
}
[IterationCount(10)]
[InvocationCount(100_000_000)]
public class Bench22
{
Random rnd = new Random();
private int x;
public int X => rnd.Next(1, 5);
[Benchmark]
[Arguments(1), Arguments(2), Arguments(3), Arguments(4)]
public int CSharp(int s) => Cond(s);
[Benchmark]
[Arguments(1), Arguments(2), Arguments(3), Arguments(4)]
public int FSharp(int s) => Fs.Bench.condition(s);
[Benchmark]
[Arguments(1), Arguments(2), Arguments(3), Arguments(4)]
public int FSharpReorder(int s) => Fs.Bench.conditionReorder(s);
[Benchmark]
[Arguments(1), Arguments(2), Arguments(3), Arguments(4)]
public int FSharpMatch(int s) => Fs.Bench.conditionMatch(s);
//
// FSharp will not inline the code so we shouldn't eiter.
//
[MethodImpl(MethodImplOptions.NoInlining)]
public static int Cond(int x)
{
if (x == 1 || x == 2) return 1;
else if (x == 3 || x == 4) return 2;
else return 0;
}
}
}
@MichalBrylka
Copy link
Author

BenchmarkDotNet=v0.12.1, OS=Windows 10.0.19041.928 (2004/?/20H1)
Intel Core i7-8700 CPU 3.20GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical cores
.NET Core SDK=5.0.201
  [Host]     : .NET Core 5.0.4 (CoreCLR 5.0.421.11614, CoreFX 5.0.421.11614), X64 RyuJIT
  Job-OMFHNN : .NET Core 5.0.4 (CoreCLR 5.0.421.11614, CoreFX 5.0.421.11614), X64 RyuJIT

InvocationCount=100000000  IterationCount=10  UnrollFactor=1  
Method s Mean Error StdDev
CSharp 1 1.3916 ns 0.0109 ns 0.0072 ns
FSharp 1 0.2426 ns 0.0235 ns 0.0140 ns
FSharpReorder 1 2.0535 ns 0.0415 ns 0.0247 ns
FSharpMatch 1 0.7153 ns 0.0189 ns 0.0125 ns
CSharp 2 0.4730 ns 0.0150 ns 0.0099 ns
FSharp 2 1.8127 ns 0.0394 ns 0.0235 ns
FSharpReorder 2 0.9916 ns 0.0999 ns 0.0594 ns
FSharpMatch 2 1.1846 ns 0.0223 ns 0.0133 ns
CSharp 3 0.9599 ns 0.0138 ns 0.0091 ns
FSharp 3 0.9485 ns 0.0174 ns 0.0103 ns
FSharpReorder 3 2.0576 ns 0.0305 ns 0.0202 ns
FSharpMatch 3 0.9495 ns 0.0263 ns 0.0174 ns
CSharp 4 0.7171 ns 0.0158 ns 0.0105 ns
FSharp 4 2.0513 ns 0.0335 ns 0.0199 ns
FSharpReorder 4 0.4725 ns 0.0171 ns 0.0113 ns
FSharpMatch 4 0.9534 ns 0.0175 ns 0.0104 ns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment