-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypesafe-performance-with-structs.fsx
92 lines (68 loc) · 1.8 KB
/
typesafe-performance-with-structs.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// type is an wrapper for a primitive type
type CustomerId = CustomerId of int
let Add1ToCustomerId (CustomerId i) =
CustomerId (i + 1)
let IsCustomerIdSmall (CustomerId i) =
i < 100000
//Use an obj wrapper
let ObjAdd1ToCustomerId (i: obj) =
(i :?> int + 1) :> obj
let ObjIsCustomerIdSmall (i: obj) =
(i :?> int) < 100000
//Use an alias
type AliasCustomerId = int
let AliasAdd1ToCustomerId (i: AliasCustomerId): AliasCustomerId =
(i + 1)
let AliasIsCustomerIdSmall (i: AliasCustomerId) =
i < 100000
//Use a struct
[<Struct>]
type StructCustomerId = StructCustomerId of int
let StructAdd1ToCustomerId (StructCustomerId i) =
StructCustomerId (i + 1)
let StructIsCustomerIdSmall (StructCustomerId i) =
i < 100000
printfn "F# wrapper classes are great for domain modeling ..."
#time
Array.init 10000000 CustomerId
// map it
|> Array.map Add1ToCustomerId
// map it again
|> Array.map Add1ToCustomerId
// filter it
|> Array.filter IsCustomerIdSmall
|> ignore
#time
printfn "But the wrappers come with instantiation overhead, boxing, and GC"
#time
Array.init 10000000 (fun x -> x :> obj)
// map it
|> Array.map ObjAdd1ToCustomerId
// map it again
|> Array.map ObjAdd1ToCustomerId
// filter it
|> Array.filter ObjIsCustomerIdSmall
|> ignore
#time
printfn "Aliasing is faster, but you lose the type safety"
#time
Array.init 10000000 (fun x -> x)
// map it
|> Array.map AliasAdd1ToCustomerId
// map it again
|> Array.map AliasAdd1ToCustomerId
// filter it
|> Array.filter AliasIsCustomerIdSmall
|> ignore
#time
printfn "Why not just use structs?"
#time
Array.init 10000000 StructCustomerId
// map it
|> Array.map StructAdd1ToCustomerId
// map it again
|> Array.map StructAdd1ToCustomerId
// filter it
|> Array.filter StructIsCustomerIdSmall
|> ignore
#time