@@ -0,0 +1,98 @@
1+module Suite exposing (main)
2+
3+import Benchmark exposing (Benchmark)
4+import Benchmark.Runner exposing (BenchmarkProgram, program)
5+import Dict
6+import Json.Encode
7+import Set
8+
9+
10+dict : Benchmark
11+dict =
12+ let
13+ dest =
14+ Dict.singleton "a" 1
15+ in
16+ Benchmark.describe "dictionary"
17+ [ Benchmark.benchmark "get" (\_ -> Dict.get "a" dest)
18+ , Benchmark.benchmark "insert" (\_ -> Dict.insert "b" 2 dest)
19+ ]
20+
21+
22+main : BenchmarkProgram
23+main =
24+ program <|
25+ Benchmark.describe "sample"
26+ [ dict
27+ , deduplications
28+ ]
29+
30+
31+deduplications =
32+ Benchmark.describe "deduplications"
33+ [ Benchmark.benchmark "fold - dedup, String concat"
34+ (\_ ->
35+ String.join ":" (foldDedup largeList)
36+ )
37+ , Benchmark.benchmark "fold - dedup"
38+ (\_ ->
39+ foldDedup largeList
40+ )
41+ , Benchmark.benchmark "fold - dedup - Int"
42+ (\_ ->
43+ foldDedup largeListInt
44+ )
45+ , Benchmark.benchmark "Json encode"
46+ (\_ ->
47+ Json.Encode.object largeListJson
48+ )
49+ ]
50+
51+
52+foldDedup list =
53+ let
54+ dedup ( name, val ) ( cache, ls ) =
55+ if Set.member name cache then
56+ ( cache, ls )
57+
58+ else
59+ ( Set.insert name cache, val :: ls )
60+ in
61+ List.foldl dedup ( Set.empty, [] ) list
62+ |> Tuple.second
63+
64+
65+largeListInt =
66+ List.range 1 10000
67+ |> List.map
68+ (\i ->
69+ let
70+ wrappingI =
71+ modBy 50 i
72+ in
73+ ( wrappingI, "elem-" ++ String.fromInt wrappingI )
74+ )
75+
76+
77+largeList =
78+ List.range 1 10000
79+ |> List.map
80+ (\i ->
81+ let
82+ wrappingI =
83+ modBy 50 i
84+ in
85+ ( "elem-" ++ String.fromInt wrappingI, "elem-" ++ String.fromInt wrappingI )
86+ )
87+
88+
89+largeListJson =
90+ List.range 1 10000
91+ |> List.map
92+ (\i ->
93+ let
94+ wrappingI =
95+ modBy 50 i
96+ in
97+ ( "elem-" ++ String.fromInt wrappingI, Json.Encode.string ("elem-" ++ String.fromInt wrappingI) )
98+ )