Skip to content

Instantly share code, notes, and snippets.

@manofstick
Created November 2, 2016 21:21
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 manofstick/1b652f76708f1e59b4f03167df318e28 to your computer and use it in GitHub Desktop.
Save manofstick/1b652f76708f1e59b4f03167df318e28 to your computer and use it in GitHub Desktop.
state machine vs piping
open System.Diagnostics
let createViaStateMachine data =
seq {
for item in data do
if item % 13L <> 0L then
let item2 = item * item
if item2 % 11L <> 0L then
yield item2 }
let createViaPiping data =
data
|> Seq.filter (fun x -> x % 13L <> 0L)
|> Seq.map (fun x -> x * x)
|> Seq.filter (fun x -> x % 11L <> 0L)
let timeTest data createSeq =
let sequence =
data
|> createSeq
let sw = Stopwatch.StartNew ()
let mutable total = 0L
for i = 1 to 100 do
let x = Seq.sum sequence
total <- total + x
printfn "%d (check=%d)" sw.ElapsedMilliseconds total
[<EntryPoint>]
let main argv =
let data = Array.init 1000000 int64
match sizeof<System.IntPtr> with
| 4 -> printfn "32 bit"
| 8 -> printfn "64 bit"
| n -> printfn "sizeof<System.IntPtr> = %d" n
printfn ""
printfn "via state machine"
for i = 1 to 5 do
timeTest data createViaStateMachine
printfn ""
printfn "via Seq.*"
for i = 1 to 5 do
timeTest data createViaPiping
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment