File: Speed.hs

package info (click to toggle)
haskell-diagrams-lib 1.4.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,256 kB
  • sloc: haskell: 8,263; makefile: 2
file content (47 lines) | stat: -rw-r--r-- 1,671 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
{-# LANGUAGE ConstraintKinds       #-}
{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE TypeFamilies          #-}

module Main where

import Criterion
import Criterion.Main (defaultMain)

import Diagrams.Prelude


main :: IO ()
main = defaultMain
    [ bgroup "rotates"
        [ bench "rotate"  $ whnf (rotate (90 @@ deg :: Angle Double)) (V2 3 3)
         ,bench "rotate1" $ whnf (rotate' (90 @@ deg :: Angle Double)) (V2 3 3)
         ,bench "rotate2" $ whnf (rotate'' (90 @@ deg :: Angle Double)) (V2 3 3)
        ]
    ]

--the original " '' " and a secondary " ' " rotate function for comparing speed testing
--note: function time changes dramatically when function is in this file rather than imported


rotation' :: Floating n => Angle n -> T2 n
rotation' theta = fromLinear r (linv r)
                where
                r               = rot theta <-> rot (negated theta)
                rot th (V2 x y) = V2 (c * x - s * y)
                                     (s * x + c * y)
                                      where
                                        c = cosA th
                                        s = sinA th

rotate' :: (InSpace V2 n t, Transformable t, Floating n) => Angle n -> t -> t
rotate'  = transform . rotation'

rotation'' :: Floating n => Angle n -> T2 n
rotation'' theta = fromLinear r (linv r)
              where
                r               = rot theta <-> rot (negated theta)
                rot th (V2 x y) = V2 (cosA th * x - sinA th * y)
                                     (sinA th * x + cosA th * y)

rotate'' :: (InSpace V2 n t, Transformable t, Floating n) => Angle n -> t -> t
rotate'' = transform . rotation''