Created
November 2, 2016 21:21
-
-
Save manofstick/1b652f76708f1e59b4f03167df318e28 to your computer and use it in GitHub Desktop.
state machine vs piping
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.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