on master help sign in
Add benchmarks for let and case expressions
commitd814a8a41cb3721a1009f57128ec02ac2e428128 authorEvan Czaplicki <info@elm-lang.org> dateWed 7 Oct 2015, 22:11 (11 years ago) parent8e22ac9
3 files changed+122−1
bench/src/Main.elm+5−1 bench/src/Syntax/Case.elm+80 bench/src/Syntax/Let.elm+37
bench/src/Main.elm+5−1
@@ -5,6 +5,8 @@ import Signal
55import Task
66
77import Benchmark
8+import Syntax.Case as Case
9+import Syntax.Let as Let
810import Syntax.Records as Records
911import Realistic.Todo as Todo
1012
@@ -14,7 +16,9 @@ import Realistic.Todo as Todo
1416compilerBenchmark : Benchmark.Benchmark
1517compilerBenchmark =
1618 Benchmark.suite "generated code"
17− [ Records.benchmark
19+ [ Case.benchmark
20+ , Let.benchmark
21+ , Records.benchmark
1822 , Todo.benchmark
1923 ]
2024
bench/src/Syntax/Case.elmnew file+80
@@ -0,0 +1,80 @@
1+module Syntax.Case (benchmark) where
2+
3+import Benchmark exposing (Benchmark, test, suite)
4+import String
5+
6+
7+benchmark : Benchmark.Benchmark
8+benchmark =
9+ suite "case expressions"
10+ [ suite "simple explicit patterns"
11+ [ test "Maybe.withDefault 0 (Just 42)" (withDefault 0 (Just 42))
12+ , test "Maybe.withDefault 0 Nothing" (withDefault 0 Nothing)
13+ , test "isBlack Diamond" (isBlack Diamond)
14+ , test "evaluate basicBoolean" (\_ -> evaluate basicBoolean)
15+ , test "evaluate fancyBoolean" (\_ -> evaluate fancyBoolean)
16+ ]
17+ ]
18+
19+
20+-- 1 Level Deep Patterns
21+
22+withDefault default maybe _ =
23+ case maybe of
24+ Just x ->
25+ x
26+
27+ Nothing ->
28+ default
29+
30+
31+type Suit = Diamond | Heart | Spade | Club
32+
33+
34+isBlack suit _ =
35+ case suit of
36+ Diamond ->
37+ False
38+
39+ Heart ->
40+ False
41+
42+ Spade ->
43+ True
44+
45+ Club ->
46+ True
47+
48+
49+type Boolean
50+ = Tru
51+ | Fls
52+ | And Boolean Boolean
53+ | Or Boolean Boolean
54+ | Not Boolean
55+
56+
57+basicBoolean =
58+ And (Not Fls) Tru
59+
60+
61+fancyBoolean =
62+ Or (Not Tru) (Or Fls (And Tru Tru))
63+
64+
65+evaluate boolean =
66+ case boolean of
67+ Tru ->
68+ True
69+
70+ Fls ->
71+ False
72+
73+ And a b ->
74+ evaluate a && evaluate b
75+
76+ Or a b ->
77+ evaluate a || evaluate b
78+
79+ Not bool ->
80+ not (evaluate bool)
bench/src/Syntax/Let.elmnew file+37
@@ -0,0 +1,37 @@
1+module Syntax.Let (benchmark) where
2+
3+import Benchmark exposing (Benchmark, test, suite)
4+import String
5+
6+
7+benchmark : Benchmark.Benchmark
8+benchmark =
9+ suite "let expressions"
10+ [ test "bind numbers, add them" simpleBindings
11+ , test "tuple destructuring" tupleDestructuring
12+ , test "record destructuring" recordDestructuring
13+ ]
14+
15+
16+simpleBindings _ =
17+ let
18+ x = 3
19+ y = 4
20+ in
21+ x + y
22+
23+
24+tupleDestructuring _ =
25+ let
26+ (x,y) = (3,4)
27+ (a,b) = (1,1)
28+ in
29+ x + y + a + b
30+
31+
32+recordDestructuring _ =
33+ let
34+ {x,y} = { x = 3, y = 4 }
35+ {a,b} = { a = 1, b = 1 }
36+ in
37+ x + y + a + b