Last active
September 25, 2021 17:51
-
-
Save palladin/aac92fe83aba3027969c to your computer and use it in GitHub Desktop.
Func<...> vs FSharpFunc<...> (2-arg functions)
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
#time | |
let sum (f : 'T -> int -> int) (array : 'T[]) = | |
let mutable sum = 0 | |
for i = 0 to array.Length - 1 do | |
sum <- sum + f array.[i] i | |
sum | |
let sum' (f : System.Func<'T, int, int>) (array : 'T[]) = | |
let mutable sum = 0 | |
for i = 0 to array.Length - 1 do | |
sum <- sum + f.Invoke(array.[i], i) | |
sum | |
let sum'' (f : ('T * int) -> int) (array : 'T[]) = | |
let mutable sum = 0 | |
for i = 0 to array.Length - 1 do | |
sum <- sum + f (array.[i], i) | |
sum | |
let data = [|1..10000000|] | |
// Real: 00:00:00.283, CPU: 00:00:00.280, GC gen0: 0, gen1: 0, gen2: 0 | |
sum (fun v i -> v + i) data | |
// Real: 00:00:00.102, CPU: 00:00:00.093, GC gen0: 0, gen1: 0, gen2: 0 | |
sum' (System.Func<_, _, _>(fun v i -> v + i)) data | |
// Real: 00:00:00.319, CPU: 00:00:00.312, GC gen0: 76, gen1: 0, gen2: 0 | |
sum'' (fun (v, i) -> v + i) data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment