diff options
author | Alberto Ruiz <aruiz@um.es> | 2007-10-22 18:43:08 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2007-10-22 18:43:08 +0000 |
commit | 477c7cfc74c93363708ece4832947455c3e55e31 (patch) | |
tree | 1c0ea4836a41615058085ce465d84f122076e8f5 /examples/speed.hs | |
parent | 1a9c07dd1fffdbd7eb939fa6a781793419947c08 (diff) |
speed tests
Diffstat (limited to 'examples/speed.hs')
-rw-r--r-- | examples/speed.hs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/examples/speed.hs b/examples/speed.hs new file mode 100644 index 0000000..a937f31 --- /dev/null +++ b/examples/speed.hs | |||
@@ -0,0 +1,50 @@ | |||
1 | import Numeric.LinearAlgebra | ||
2 | import System | ||
3 | import Data.List(foldl1') | ||
4 | import System.CPUTime | ||
5 | import Text.Printf | ||
6 | import Debug.Trace | ||
7 | |||
8 | debug x = trace (show x) x | ||
9 | |||
10 | timing act = do | ||
11 | t0 <- getCPUTime | ||
12 | act | ||
13 | t1 <- getCPUTime | ||
14 | printf "%.2f CPU seconds\n" $ (fromIntegral ((t1 - t0) `div` (10^10)) / 100 :: Double) | ||
15 | |||
16 | op a b = trans $ (trans a) <> (trans b) | ||
17 | |||
18 | op2 a b = trans $ (trans a) + (trans b) | ||
19 | |||
20 | rot' :: Double -> Matrix Double | ||
21 | rot' a = matrix [[ c,0,s], | ||
22 | [ 0,1,0], | ||
23 | [-s,0,c]] | ||
24 | where c = cos a | ||
25 | s = sin a | ||
26 | matrix = fromLists | ||
27 | |||
28 | rot :: Double -> Matrix Double | ||
29 | rot a = (3><3) [ c,0,s | ||
30 | , 0,1,0 | ||
31 | ,-s,0,c ] | ||
32 | where c = cos a | ||
33 | s = sin a | ||
34 | |||
35 | |||
36 | fun n r = foldl1' (<>) (map r [0,delta..1]) where delta = 1 /(fromIntegral n-1) | ||
37 | |||
38 | |||
39 | main = do | ||
40 | args <- getArgs | ||
41 | let [p,n,d] = map read args | ||
42 | let ms = replicate n ((ident d :: Matrix Double)) | ||
43 | let mz = replicate n (diag (constant (0::Double) d)) | ||
44 | timing $ case p of | ||
45 | 0 -> print $ foldl1' (<>) ms | ||
46 | 1 -> print $ foldl1' (<>) (map trans ms) | ||
47 | 2 -> print $ foldl1' op ms | ||
48 | 3 -> print $ foldl1' op2 mz | ||
49 | 4 -> print $ fun n rot' | ||
50 | 5 -> print $ fun n rot | ||