Last active
April 20, 2021 13:19
-
-
Save MichalBrylka/84887188e887b7cb13d53035766a6ad8 to your computer and use it in GitHub Desktop.
Cs vs Fs @ branching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Author
MichalBrylka
commented
Apr 20, 2021
Method | s | Mean | Error | StdDev |
---|---|---|---|---|
CSharp | 1 | 0.7398 ns | 0.0417 ns | 0.0276 ns |
FSharp | 1 | 0.4870 ns | 0.0390 ns | 0.0258 ns |
FSharpReorder | 1 | 2.2707 ns | 0.0531 ns | 0.0316 ns |
FSharpMatch | 1 | 0.7105 ns | 0.0209 ns | 0.0124 ns |
CSharp | 2 | 0.4951 ns | 0.0331 ns | 0.0219 ns |
FSharp | 2 | 2.0574 ns | 0.0248 ns | 0.0148 ns |
FSharpReorder | 2 | 0.9611 ns | 0.0341 ns | 0.0203 ns |
FSharpMatch | 2 | 1.4368 ns | 0.0241 ns | 0.0144 ns |
CSharp | 3 | 1.8718 ns | 0.0214 ns | 0.0142 ns |
FSharp | 3 | 0.9779 ns | 0.0458 ns | 0.0303 ns |
FSharpReorder | 3 | 1.8264 ns | 0.0332 ns | 0.0174 ns |
FSharpMatch | 3 | 0.9543 ns | 0.0253 ns | 0.0167 ns |
CSharp | 4 | 0.7147 ns | 0.0237 ns | 0.0141 ns |
FSharp | 4 | 2.0573 ns | 0.0416 ns | 0.0275 ns |
FSharpReorder | 4 | 0.2434 ns | 0.0214 ns | 0.0127 ns |
FSharpMatch | 4 | 0.7032 ns | 0.0239 ns | 0.0142 ns |
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