on master help sign in
Added dictionary benchmarks
commita916cc174ef3faf656505580d25dfcd81c5c4e75 authorjmite <jmitdase@gmail.com> dateThu 20 Aug 2015, 15:28 (11 years ago) parent3434bbd
2 files changed+76
bench/CompilerBenchmarks.elm+13 bench/src/DictBench.elm+63
bench/CompilerBenchmarks.elm+13
@@ -5,6 +5,7 @@ import Task exposing (Task, andThen)
55import Text
66import Array
77import Random
8+import Dict
89import Time
910
1011import LargeDictionary
@@ -19,6 +20,7 @@ import CtorBench
1920import SmallCtorBench
2021import PatternMatchBench
2122import IfBench
23+import DictBench
2224
2325{-| Compile this with --output=Benchmarks.html to generate
2426 a webpage, which runs the benchmarks and shows the results.
@@ -53,6 +55,10 @@ mySuite =
5355 , Benchmark.bench1 "Array custom foldl, 500 elems: " ArrayBench.customFoldrMeanStddev array500
5456 , Benchmark.bench3 "Array square nth elem, 500 elems: " ArrayBench.updateNRandomIndices (\x -> x*x) indices200 array500
5557
58+ , Benchmark.bench1 "Dict native foldr, 500 elems: " DictBench.foldrMeanStddev dict500
59+ , Benchmark.bench1 "Dict native foldl, 500 elems: " DictBench.foldlMeanStddev dict500
60+ , Benchmark.bench3 "Dict square nth elem, 500 elems: " DictBench.updateNRandomIndices (\x -> x*x) indices200 dict500
61+
5662 , Benchmark.bench1 "List native foldr, 500 elems: " ListBench.foldrMeanStddev list500
5763 , Benchmark.bench1 "List native foldl, 500 elems: " ListBench.foldlMeanStddev list500
5864 , Benchmark.bench1 "List custom foldr, 500 elems: " ListBench.customFoldrMeanStddev list500
@@ -61,6 +67,7 @@ mySuite =
6167
6268 , Benchmark.bench2 "200 lookups in array of 500 elems: " ArrayBench.randomLookups indices200 (Array.fromList [1 .. 500] )
6369 , Benchmark.bench2 "200 lookups in list of 500 elems: " ListBench.randomLookups indices200 ([1 .. 500] )
70+ , Benchmark.bench2 "200 lookups in dict of 500 elems: " DictBench.randomLookups indices200 (Dict.fromList (List.map2 (,) [1 .. 500] [1 .. 500]) )
6471
6572 , Benchmark.bench1 "30s simulated animation @ 30fps: " AnimateBench.runAnimation timeSteps30s
6673 ]
@@ -120,6 +127,12 @@ seed =
120127--Pseudo-random array of 500 floating point numbers
121128array500 =
122129 (ArrayBench.randomArray floatGen 500 seed)
130+
131+
132+--Pseudo-random dictionary, indexed 0 .. 500, of 500 floating point numbers
133+dict500 =
134+ (DictBench.randomDict floatGen 500 seed)
135+
123136
124137
125138--Same contents as array500, but in list form
bench/src/DictBench.elmnew file+63
@@ -0,0 +1,63 @@
1+module DictBench where
2+
3+import Dict
4+import Random
5+import Debug exposing (crash)
6+
7+
8+
9+randomDict
10+ : Random.Generator a
11+ -> Int
12+ -> Random.Seed
13+ -> Dict.Dict Int a
14+randomDict gen n initSeed =
15+ let
16+ f n (seed, accum) =
17+ let
18+ (nextToAdd, newSeed) = Random.generate gen seed
19+ newAccum = Dict.insert n nextToAdd accum
20+ in (newSeed, newAccum)
21+ in
22+ snd <| List.foldl f (initSeed, Dict.empty) [1 .. n]
23+
24+meanAndStddev
25+ : ((Int -> Float -> (Float, Float) -> (Float, Float)) -> (Float, Float) -> Dict.Dict Int Float -> (Float, Float))
26+ -> Dict.Dict Int Float
27+ -> (Float, Float)
28+meanAndStddev foldFn ourDict =
29+ let
30+ --ourDict = randomDict n (Random.initialSeed 23)
31+ (theSum, sumSq) = foldFn (\_ x (sum, sumSq) -> (x + sum, (x^2) + sumSq) ) (0,0) ourDict
32+ in
33+ (theSum, sqrt(theSum*theSum - sumSq))
34+
35+
36+foldrMeanStddev = meanAndStddev Dict.foldr
37+
38+
39+foldlMeanStddev = meanAndStddev Dict.foldl
40+
41+
42+updateNRandomIndices
43+ : (Float -> Float)
44+ -> List Int
45+ -> Dict.Dict Int Float
46+ -> Dict.Dict Int Float
47+updateNRandomIndices f indexList startList =
48+ List.foldr
49+ (\ i oldList -> updateN i f oldList)
50+ startList indexList
51+
52+
53+randomLookups : List Int -> Dict.Dict Int Float -> List (Maybe Float)
54+randomLookups indices arr =
55+ List.map (\i -> Dict.get i arr) indices
56+
57+
58+
59+updateN : Int -> (a -> a) -> Dict.Dict Int a -> Dict.Dict Int a
60+updateN n f arr =
61+ case Dict.get n arr of
62+ Just x -> Dict.insert n (f x) arr
63+ _ -> crash "Dict out of bounds"