on master help sign in
Add animation benchmark
7414760jmite
bench/CompilerBenchmarks.elm+18−5
@@ -8,6 +8,7 @@ import Array
8import LargeDictionary
8import LargeDictionary
9import ArrayBench
9import ArrayBench
10import ListBench
10import ListBench
11+import AnimateBench
11import Random
12import Random
12import Time
13import Time
13{-| Compile this with --output=Benchmarks.html to generate
14{-| Compile this with --output=Benchmarks.html to generate
@@ -34,6 +35,8 @@ mySuite =
34
35
35 , Benchmark.bench2 "200 lookups in array of 500 elems: " ArrayBench.randomLookups indices200 (Array.fromList [1 .. 500] )
36 , Benchmark.bench2 "200 lookups in array of 500 elems: " ArrayBench.randomLookups indices200 (Array.fromList [1 .. 500] )
36 , Benchmark.bench2 "200 lookups in list of 500 elems: " ListBench.randomLookups indices200 ([1 .. 500] )
37 , Benchmark.bench2 "200 lookups in list of 500 elems: " ListBench.randomLookups indices200 ([1 .. 500] )
38+
39+ , Benchmark.bench1 "30s simulated animation: " AnimateBench.runAnimation timeSteps30s
37 ]
40 ]
38
41
39
42
@@ -82,21 +85,31 @@ indexGen =
82--across multiple runs of the benchmark
85--across multiple runs of the benchmark
83
86
84--seed for our index generation
87--seed for our index generation
88+indexSeed =
85−indexSeed = Random.initialSeed 56
89+ Random.initialSeed 56
86
90
87
91
88--Seed for our float generation
92--Seed for our float generation
93+seed =
89−seed = Random.initialSeed 23
94+ Random.initialSeed 23
90
95
91
96
92--Pseudo-random array of 500 floating point numbers
97--Pseudo-random array of 500 floating point numbers
98+array500 =
93−array500 = (ArrayBench.randomArray floatGen 500 seed)
99+ (ArrayBench.randomArray floatGen 500 seed)
94
100
95
101
96--Same contents as array500, but in list form
102--Same contents as array500, but in list form
103+list500 =
97−list500 = (ListBench.randomList floatGen 500 seed)
104+ (ListBench.randomList floatGen 500 seed)
98
105
99
106
100--Pseudo random list of 200 indices between 0 and 499
107--Pseudo random list of 200 indices between 0 and 499
108+indices200 =
101−indices200 = (ListBench.randomList indexGen 200 indexSeed)
109+ (ListBench.randomList indexGen 200 indexSeed)
110+
111+
112+--List of timesteps simulating 30 seconds of animation at 30fps
113+timeSteps30s =
114+ List.map (\x -> (1.0 / 30.0) * (toFloat x * Time.second) ) [1 .. 900]
102
115
bench/src/AnimateBench.elmnew file+58
@@ -0,0 +1,58 @@
1+module AnimateBench where
2+
3+import Time exposing (Time)
4+
5+
6+{-| Main Spring Type.
7+A spring's behavior is defined by its stiffness and damping parameters.
8+-}
9+type alias Spring a =
10+ { stiffness : Float
11+ , damping : Float
12+ , position : a
13+ , velocity : a
14+ , destination : a
15+ }
16+
17+
18+defaultSpring =
19+ Spring 1.0 0.5 0.0 1.0 100.0
20+
21+
22+epsilon = 0.0001
23+
24+
25+{-| Animate a spring given a framerate.
26+
27+ animate framerate spring
28+-}
29+animate : Time -> Spring Float -> Spring Float
30+animate fpms spring =
31+ let
32+ frameRate = fpms / 1000
33+
34+ fspring = -spring.stiffness * (spring.position - spring.destination)
35+
36+ fdamper = -spring.damping * spring.velocity
37+
38+ a = fspring + fdamper
39+
40+ newV = spring.velocity + a * frameRate
41+ newX = spring.position + newV * frameRate
42+ in
43+ if
44+ abs (newV - spring.velocity) < epsilon && abs (newX - spring.position) < epsilon
45+ then
46+ { spring | position <- spring.destination
47+ , velocity <- 0
48+ }
49+ else
50+ { spring | position <- newX
51+ , velocity <- newV
52+ }
53+
54+
55+
56+runAnimation : List Float -> Spring Float
57+runAnimation timeSteps =
58+ List.foldr animate defaultSpring timeSteps