on master help sign in
Add animation benchmark
commit7414760ac38018a4c5eeb86387a4253c410fca79 authorjmite <jmitdase@gmail.com> dateThu 20 Aug 2015, 10:40 (11 years ago) parent0d207d0
2 files changed+76−5
bench/CompilerBenchmarks.elm+18−5 bench/src/AnimateBench.elm+58
bench/CompilerBenchmarks.elm+18−5
@@ -8,6 +8,7 @@ import Array
88import LargeDictionary
99import ArrayBench
1010import ListBench
11+import AnimateBench
1112import Random
1213import Time
1314{-| Compile this with --output=Benchmarks.html to generate
@@ -34,6 +35,8 @@ mySuite =
3435
3536 , Benchmark.bench2 "200 lookups in array of 500 elems: " ArrayBench.randomLookups indices200 (Array.fromList [1 .. 500] )
3637 , Benchmark.bench2 "200 lookups in list of 500 elems: " ListBench.randomLookups indices200 ([1 .. 500] )
38+
39+ , Benchmark.bench1 "30s simulated animation: " AnimateBench.runAnimation timeSteps30s
3740 ]
3841
3942
@@ -82,21 +85,31 @@ indexGen =
8285--across multiple runs of the benchmark
8386
8487--seed for our index generation
85−indexSeed = Random.initialSeed 56
88+indexSeed =
89+ Random.initialSeed 56
8690
8791
8892--Seed for our float generation
89−seed = Random.initialSeed 23
93+seed =
94+ Random.initialSeed 23
9095
9196
9297--Pseudo-random array of 500 floating point numbers
93−array500 = (ArrayBench.randomArray floatGen 500 seed)
98+array500 =
99+ (ArrayBench.randomArray floatGen 500 seed)
94100
95101
96102--Same contents as array500, but in list form
97−list500 = (ListBench.randomList floatGen 500 seed)
103+list500 =
104+ (ListBench.randomList floatGen 500 seed)
98105
99106
100107--Pseudo random list of 200 indices between 0 and 499
101−indices200 = (ListBench.randomList indexGen 200 indexSeed)
108+indices200 =
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]
102115
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