Created
February 15, 2022 14:31
-
-
Save matthewcrews/421c25a9696f18aaf8153d0d7f24df66 to your computer and use it in GitHub Desktop.
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
open System | |
open BenchmarkDotNet.Attributes | |
open BenchmarkDotNet.Running | |
open BenchmarkDotNet.Diagnosers | |
[<HardwareCounters(HardwareCounter.BranchMispredictions, HardwareCounter.CacheMisses)>] | |
[<DisassemblyDiagnoser(printSource = true, exportHtml = true)>] | |
type Benchmarks () = | |
let rng = Random 123 | |
let arraySize = 1_000 | |
let lookupValues = | |
[| for _ = 1 to arraySize do rng.Next (0, 100) |] | |
let arrayOrderedFast (a: array<int>) = | |
let mutable result = 0 | |
let mutable i = 0 | |
while i < a.Length - 1 do | |
result <- a[i] | |
i <- i + 1 | |
result | |
[<Benchmark>] | |
member _.ArrayOrderedSlow () = | |
let mutable result = 0 | |
let mutable i = 0 | |
while i < lookupValues.Length - 1 do | |
result <- lookupValues[i] | |
i <- i + 1 | |
result | |
[<Benchmark>] | |
member _.ArrayOrderedFast () = | |
arrayOrderedFast lookupValues |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment