Created
March 26, 2017 20:52
-
-
Save manofstick/0b80bba196991bc5c45bc37dd5b75d9d 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.Diagnostics | |
let sum1 (a:array<_>) total = | |
total := 0 | |
for i = 0 to a.Length-1 do | |
total := !total + a.[i] | |
!total | |
let sum2 (a:array<_>) total = | |
total := 0 | |
let mutable i = 0 | |
while i < a.Length do | |
total := !total + a.[i] | |
i <- i + 1 | |
!total | |
let sum3 (a:array<_>) total = | |
total := 0 | |
let rec iterate i = | |
if i < a.Length then | |
total := !total + a.[i] | |
iterate (i+1) | |
iterate 0 | |
!total | |
let t f a = | |
let sw = Stopwatch.StartNew () | |
let total = ref 0 | |
if f a total <> 10000000 then | |
failwith "boom!" | |
sw.ElapsedMilliseconds | |
[<EntryPoint>] | |
let main argv = | |
let data = Array.zeroCreate 10000000 | |
for i = 0 to data.Length-1 do | |
data.[i] <- 1 | |
let z = t sum1 data | |
let z = t sum2 data | |
let z = t sum3 data | |
let mutable _1 = 0L | |
let mutable _2 = 0L | |
let mutable _3 = 0L | |
for i = 1 to 5 do | |
for i = 1 to 50 do | |
_1 <- _1 + t sum1 data | |
_2 <- _2 + t sum2 data | |
_3 <- _3 + t sum3 data | |
printfn "%d\t%d\t%d" _1 _2 _3 | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment