Skip to content

Instantly share code, notes, and snippets.

@manofstick
Last active June 17, 2018 06:27
Show Gist options
  • Save manofstick/46ae43f5969cc1bb4c125509703299d5 to your computer and use it in GitHub Desktop.
Save manofstick/46ae43f5969cc1bb4c125509703299d5 to your computer and use it in GitHub Desktop.
list<option<type>>
open System.Diagnostics
open Perf
type TestRecord = { A : int; B : float }
type TestGenericRecord<'a, 'b> = { AA : 'a; BB : 'b }
type TestUnion =
| Blah
| FloatThing of float
| Thing of int
type TestGenericUnion<'a, 'b> =
| BBlah
| FFloatThing of 'a
| TThing of 'b
let createData special normal = Array.init 1000 (fun i ->
if i &&& 433 = 433
then [Some special]
elif i &&& 439 = 439
then [None]
else List.init (i%10) (fun i -> Some (normal i)))
let inline runTest theType (data:array<'T>) =
let iterations = 5
let mutable totalTime = 0L
let mutable i = 0
let mutable j = 0
for iteration = 1 to iterations do
let sw = Stopwatch.StartNew ()
let mutable count = 0
for __ = 1 to 10000000 do
i <- if i = 991 then 0 else i + 1
j <- if j = 997 then 0 else j + 1
if data.[i] = data.[j] then
count <- count + 1
let time = sw.ElapsedMilliseconds
totalTime <- totalTime + time
printfn "%s: %d (checksum#%d=%d)" theType time iteration count
theType, (int (totalTime/int64 iterations))
let testInt () =
let intData = createData -1 int
runTest "int" intData
let testFloat () =
let floatData = createData System.Double.NaN float
runTest "float" floatData
let testValueTupleIntInt () =
let valueTupleData = createData (struct (-1, -1)) (fun i -> struct ((i % 23), (i % 27)))
runTest "struct int*int" valueTupleData
let testValueTupleIntInt64 () =
let valueTupleData = createData (struct (-1, -1L)) (fun i -> struct ((i % 23), int64 (i % 27)))
runTest "struct int*int64" valueTupleData
let testValueTupleIntFloat () =
let valueTupleData = createData (struct (-1, System.Double.NaN)) (fun i -> struct (i % 23, float (i % 27)))
runTest "struct int*float" valueTupleData
let testValueTupleInt64Int64Int64Int64Int64Int64Int64Int64 () =
let valueTupleData = createData (struct (-1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L)) (fun i -> struct (int64 (i % 2), int64 (i % 3), int64 (i % 5), int64 (i % 7), int64 (i % 11), int64 (i % 13), int64 (i % 17), int64 (i % 19)))
runTest "struct int64*int64*int64*int64*int64*int64*int64*int64" valueTupleData
let testValueTupleFloatFloatFloatFloatFloatFloatFloatFloat () =
let valueTupleData = createData (struct (-1., -1., -1., -1., -1., -1., -1., -1.)) (fun i -> struct (float (i % 2), float (i % 3), float (i % 5), float (i % 7), float (i % 11), float (i % 13), float (i % 17), float (i % 19)))
runTest "struct float*float*float*float*float*float*float*float" valueTupleData
let testTupleIntInt () =
let tupleData = createData ((-1, -1)) (fun i -> (i % 23, i % 27))
runTest "int*int" tupleData
let testTupleIntFloat () =
let tupleData = createData ((-1, System.Double.NaN)) (fun i -> (i % 23, float (i % 27)))
runTest "int*float" tupleData
let testTupleInt64Int64Int64Int64Int64Int64Int64Int64 () =
let valueTupleData = createData (-1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L) (fun i -> (int64 (i % 2), int64 (i % 3), int64 (i % 5), int64 (i % 7), int64 (i % 11), int64 (i % 13), int64 (i % 17), int64 (i % 19)))
runTest "int64*int64*int64*int64*int64*int64*int64*int64" valueTupleData
let testTupleFloatFloatFloatFloatFloatFloatFloatFloat () =
let valuetupleData = createData (-1., -1., -1., -1., -1., -1., -1., -1.) (fun i -> (float (i % 2), float (i % 3), float (i % 5), float (i % 7), float (i % 11), float (i % 13), float (i % 17), float (i % 19)))
runTest "float*float*float*float*float*float*float*float" valuetupleData
let testRecordData () =
let testData = createData { A = -1; B = System.Double.NaN } (fun i -> {A = i % 23; B= float (i % 27)})
runTest "TestRecordData" testData
let testGenericRecordData () =
let testData = createData { AA = -1; BB = System.Double.NaN } (fun i -> {AA = i % 23; BB= float (i % 27)})
runTest "TestGenericRecordData" testData
let testUnionData () =
let testData = createData (FloatThing System.Double.NaN) (fun i -> match i % 3 with 0 -> Blah | 1 -> Thing i | _ -> FloatThing (float i))
runTest "TestUnionData" testData
let testGenericUnionData () =
let testData = createData (FFloatThing System.Double.NaN) (fun i -> match i % 3 with 0 -> BBlah | 1 -> TThing i | _ -> FFloatThing (float i))
runTest "TestGenericUnionData" testData
let results = [
testInt ()
testFloat ()
testValueTupleIntInt ()
testValueTupleIntInt64 ()
testValueTupleIntFloat ()
testValueTupleInt64Int64Int64Int64Int64Int64Int64Int64 ()
testValueTupleFloatFloatFloatFloatFloatFloatFloatFloat ()
testTupleIntInt ()
testTupleIntFloat ()
testTupleInt64Int64Int64Int64Int64Int64Int64Int64 ()
testTupleFloatFloatFloatFloatFloatFloatFloatFloat ()
testRecordData ()
testGenericRecordData ()
testUnionData ()
testGenericUnionData ()
]
printfn ""
printfn "%s" Id.Name
results |> List.iter (fun (n,t) -> printfn "%60s | %7d | " n t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment