Skip to content

Instantly share code, notes, and snippets.

@matthewcrews
Created February 15, 2022 14:31
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 matthewcrews/421c25a9696f18aaf8153d0d7f24df66 to your computer and use it in GitHub Desktop.
Save matthewcrews/421c25a9696f18aaf8153d0d7f24df66 to your computer and use it in GitHub Desktop.
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