on master help sign in
Added pattern match benchmarks
commit4cb30244475ab6524e404c92a22715ad71318fe5 authorjmite <jmitdase@gmail.com> dateThu 20 Aug 2015, 14:34 (11 years ago) parenta25405b
2 files changed+82−1
bench/CompilerBenchmarks.elm+12−1 bench/src/PatternMatchBench.elm+70
bench/CompilerBenchmarks.elm+12−1
@@ -17,6 +17,7 @@ import TupleBench
1717import SmallTupleBench
1818import CtorBench
1919import SmallCtorBench
20+import PatternMatchBench
2021
2122{-| Compile this with --output=Benchmarks.html to generate
2223 a webpage, which runs the benchmarks and shows the results.
@@ -27,11 +28,17 @@ mySuite =
2728 Benchmark.Suite "My Suite"
2829 [ Benchmark.bench1 "Access 20-field record 200 times: " RecordBench.sumRandomElems indices200
2930 , Benchmark.bench1 "Access 9-field record 200 times: " SmallRecordBench.sumRandomElems indices200
31+
3032 , Benchmark.bench1 "Access 9-field tuple 200 times: " TupleBench.sumRandomElems indices200
3133 , Benchmark.bench1 "Access 3-field tuple 200 times: " SmallTupleBench.sumRandomElems indices200
34+
3235 , Benchmark.bench1 "Access 20-constructor Type 200 times: " CtorBench.sumFieldsList ctorData200
3336 , Benchmark.bench1 "Access 10-constructor Type 200 times: " SmallCtorBench.sumFieldsList smallCtorData200
3437
38+ , Benchmark.bench1 "Pattern match 18-levels deep, use all values, 200 times " PatternMatchBench.deepMatchSum nestedData200
39+ , Benchmark.bench1 "Pattern match 18-levels deep, 16 wildcards, 200 times " PatternMatchBench.wildcardMatchSum nestedData200
40+ , Benchmark.bench1 "Pattern match 4-levels deep, use all values, 200 times " PatternMatchBench.shallowMatchSum nestedData200
41+
3542 , Benchmark.bench1 "Make and sum dict, 10000 elems: " LargeDictionary.addNToDictAndSum 10000
3643 , Benchmark.bench1 "Make and sum dict, 50 elems: " LargeDictionary.addNToDictAndSum 50
3744
@@ -129,4 +136,8 @@ ctorData200 =
129136
130137
131138smallCtorData200 =
132− List.map SmallCtorBench.makeRandomData indices200
139+ List.map SmallCtorBench.makeRandomData indices200
140+
141+
142+nestedData200 =
143+ List.map PatternMatchBench.intToNest indices200
bench/src/PatternMatchBench.elmnew file+70
@@ -0,0 +1,70 @@
1+module PatternMatchBench where
2+
3+type NestedStructure =
4+ Nest Float (NestedStructure)
5+ | End Float
6+
7+
8+intToNest : Int -> NestedStructure
9+intToNest i =
10+ let
11+ helper i accum =
12+ case (i % 20) of
13+ 0 ->
14+ accum
15+
16+ _ ->
17+ helper (i-1) <| Nest (toFloat <| i * i) accum
18+ in
19+ helper i (End <| toFloat i)
20+
21+deepMatchingFunction : NestedStructure -> Float
22+deepMatchingFunction struct =
23+ case struct of
24+ (Nest x1 (Nest x2 (Nest x3 (Nest x4 (Nest x5 (Nest x6 (Nest x7 (Nest x8 (Nest x9 (Nest x10 (Nest x11 (Nest x12 (Nest x13 (Nest x14 (Nest x15 (Nest x16 (Nest x17 (End x18 ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ->
25+ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 + x12 + x13 + x14 + x15 + x16 + x17 + x18
26+
27+ Nest x _ ->
28+ x
29+
30+ End x ->
31+ x
32+
33+
34+wildcardMatchingFunction : NestedStructure -> Float
35+wildcardMatchingFunction struct =
36+ case struct of
37+ (Nest x (Nest _ (Nest _ (Nest _ (Nest _ (Nest _ (Nest _ (Nest _ (Nest _ (Nest _ (Nest _ (Nest _ (Nest _ (Nest _ (Nest _ (Nest _ (Nest _ (End y ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ->
38+ x + y + x + y + x + y + x + y + x + y + x + y + x + y + x + y + x + y
39+
40+ Nest x _ ->
41+ x
42+
43+ End x ->
44+ x
45+
46+
47+shallowMatchingFunction : NestedStructure -> Float
48+shallowMatchingFunction struct =
49+ case struct of
50+ (Nest x1 (Nest x2 (Nest x3 (End x4) ) ) ) ->
51+ x1 + x2 + x3 + x4 + x1 + x2 + x3 + x4 + x1 + x2 + x3 + x4 + x1 + x2 + x3 + x4 + x1 + x2
52+
53+ Nest x _ ->
54+ x
55+
56+ End x ->
57+ x
58+
59+sumStructures : (NestedStructure -> Float) -> List NestedStructure -> Float
60+sumStructures f inList =
61+ List.foldr (\x sumSoFar -> sumSoFar + f x) 0.0 inList
62+
63+
64+deepMatchSum = sumStructures deepMatchingFunction
65+
66+
67+shallowMatchSum = sumStructures shallowMatchingFunction
68+
69+
70+wildcardMatchSum = sumStructures wildcardMatchingFunction