on master help sign in
Added dictionary benchmarks
a916cc1jmite
bench/CompilerBenchmarks.elm+13
@@ -5,6 +5,7 @@ import Task exposing (Task, andThen)
5import Text
5import Text
6import Array
6import Array
7import Random
7import Random
8+import Dict
8import Time
9import Time
9
10
10import LargeDictionary
11import LargeDictionary
@@ -19,6 +20,7 @@ import CtorBench
19import SmallCtorBench
20import SmallCtorBench
20import PatternMatchBench
21import PatternMatchBench
21import IfBench
22import IfBench
23+import DictBench
22
24
23{-| Compile this with --output=Benchmarks.html to generate
25{-| Compile this with --output=Benchmarks.html to generate
24 a webpage, which runs the benchmarks and shows the results.
26 a webpage, which runs the benchmarks and shows the results.
@@ -53,6 +55,10 @@ mySuite =
53 , Benchmark.bench1 "Array custom foldl, 500 elems: " ArrayBench.customFoldrMeanStddev array500
55 , Benchmark.bench1 "Array custom foldl, 500 elems: " ArrayBench.customFoldrMeanStddev array500
54 , Benchmark.bench3 "Array square nth elem, 500 elems: " ArrayBench.updateNRandomIndices (\x -> x*x) indices200 array500
56 , Benchmark.bench3 "Array square nth elem, 500 elems: " ArrayBench.updateNRandomIndices (\x -> x*x) indices200 array500
55
57
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+
56 , Benchmark.bench1 "List native foldr, 500 elems: " ListBench.foldrMeanStddev list500
62 , Benchmark.bench1 "List native foldr, 500 elems: " ListBench.foldrMeanStddev list500
57 , Benchmark.bench1 "List native foldl, 500 elems: " ListBench.foldlMeanStddev list500
63 , Benchmark.bench1 "List native foldl, 500 elems: " ListBench.foldlMeanStddev list500
58 , Benchmark.bench1 "List custom foldr, 500 elems: " ListBench.customFoldrMeanStddev list500
64 , Benchmark.bench1 "List custom foldr, 500 elems: " ListBench.customFoldrMeanStddev list500
@@ -61,6 +67,7 @@ mySuite =
61
67
62 , Benchmark.bench2 "200 lookups in array of 500 elems: " ArrayBench.randomLookups indices200 (Array.fromList [1 .. 500] )
68 , Benchmark.bench2 "200 lookups in array of 500 elems: " ArrayBench.randomLookups indices200 (Array.fromList [1 .. 500] )
63 , Benchmark.bench2 "200 lookups in list of 500 elems: " ListBench.randomLookups indices200 ([1 .. 500] )
69 , 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]) )
64
71
65 , Benchmark.bench1 "30s simulated animation @ 30fps: " AnimateBench.runAnimation timeSteps30s
72 , Benchmark.bench1 "30s simulated animation @ 30fps: " AnimateBench.runAnimation timeSteps30s
66 ]
73 ]
@@ -120,6 +127,12 @@ seed =
120--Pseudo-random array of 500 floating point numbers
127--Pseudo-random array of 500 floating point numbers
121array500 =
128array500 =
122 (ArrayBench.randomArray floatGen 500 seed)
129 (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+
123
136
124
137
125--Same contents as array500, but in list form
138--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"