diff options
-rw-r--r-- | examples/benchmarks.hs | 50 |
1 files changed, 42 insertions, 8 deletions
diff --git a/examples/benchmarks.hs b/examples/benchmarks.hs index 7fea3ed..5c52b3c 100644 --- a/examples/benchmarks.hs +++ b/examples/benchmarks.hs | |||
@@ -2,18 +2,20 @@ | |||
2 | 2 | ||
3 | -- compile as: | 3 | -- compile as: |
4 | -- ghc --make -O2 -optc-O2 -fvia-C benchmarks.hs -o benchmarks | 4 | -- ghc --make -O2 -optc-O2 -fvia-C benchmarks.hs -o benchmarks |
5 | -- ghc --make -O benchmarks.hs -o benchmarks | ||
5 | 6 | ||
6 | import Numeric.LinearAlgebra | 7 | import Numeric.LinearAlgebra |
7 | import System.Time | 8 | import System.Time |
8 | import System.CPUTime | 9 | import System.CPUTime |
9 | import Text.Printf | 10 | import Text.Printf |
11 | import Data.List(foldl1') | ||
10 | 12 | ||
11 | 13 | ||
12 | time act = do | 14 | time act = do |
13 | t0 <- getCPUTime | 15 | t0 <- getCPUTime |
14 | act | 16 | act |
15 | t1 <- getCPUTime | 17 | t1 <- getCPUTime |
16 | putStrLn $ show ((t1-t0) `div` 10^10) ++ " cs" | 18 | printf "%.2f CPU seconds\n" $ (fromIntegral ((t1 - t0) `div` (10^10)) / 100 :: Double) :: IO () |
17 | 19 | ||
18 | time' act = do | 20 | time' act = do |
19 | t0 <- getClockTime | 21 | t0 <- getClockTime |
@@ -23,16 +25,18 @@ time' act = do | |||
23 | 25 | ||
24 | -------------------------------------------------------------------------------- | 26 | -------------------------------------------------------------------------------- |
25 | 27 | ||
26 | main = sequence_ [bench1] | 28 | main = sequence_ [bench1,bench2] |
29 | |||
30 | w :: Vector Double | ||
31 | w = constant 1 30000000 | ||
27 | 32 | ||
28 | bench1 = do | 33 | bench1 = do |
29 | putStrLn "sum of a vector with 30 million doubles:" | 34 | putStrLn "sum of a vector with 30 million doubles:" |
30 | time $ printf " BLAS: %.2f: " $ sumV sumVB 30000000 | 35 | print$ vectorMax w -- evaluate it |
31 | time $ printf " Haskell: %.2f: " $ sumV sumVH 30000000 | 36 | time $ printf " BLAS: %.2f: " $ sumVB w |
32 | time $ printf " BLAS: %.2f: " $ sumV sumVB 30000000 | 37 | time $ printf " Haskell: %.2f: " $ sumVH w |
33 | time $ printf " Haskell: %.2f: " $ sumV sumVH 30000000 | 38 | time $ printf " BLAS: %.2f: " $ sumVB w |
34 | 39 | time $ printf " Haskell: %.2f: " $ sumVH w | |
35 | sumV f n = f (constant (1::Double) n) | ||
36 | 40 | ||
37 | sumVB v = constant 1 (dim v) <.> v | 41 | sumVB v = constant 1 (dim v) <.> v |
38 | 42 | ||
@@ -42,3 +46,33 @@ sumVH v = go (d - 1) 0 | |||
42 | go :: Int -> Double -> Double | 46 | go :: Int -> Double -> Double |
43 | go 0 s = s + (v @> 0) | 47 | go 0 s = s + (v @> 0) |
44 | go !j !s = go (j - 1) (s + (v @> j)) | 48 | go !j !s = go (j - 1) (s + (v @> j)) |
49 | |||
50 | -------------------------------------------------------------------------------- | ||
51 | |||
52 | bench2 = do | ||
53 | putStrLn "-------------------------------------------------------" | ||
54 | putStrLn "multiplication of one million different 3x3 matrices" | ||
55 | putStrLn "from [[]]" | ||
56 | time $ print $ fun (10^6) rot' | ||
57 | putStrLn "from []" | ||
58 | time $ print $ fun (10^6) rot | ||
59 | print $ cos (10^6/2) | ||
60 | |||
61 | |||
62 | rot' :: Double -> Matrix Double | ||
63 | rot' a = matrix [[ c,0,s], | ||
64 | [ 0,1,0], | ||
65 | [-s,0,c]] | ||
66 | where c = cos a | ||
67 | s = sin a | ||
68 | matrix = fromLists | ||
69 | |||
70 | rot :: Double -> Matrix Double | ||
71 | rot a = (3><3) [ c,0,s | ||
72 | , 0,1,0 | ||
73 | ,-s,0,c ] | ||
74 | where c = cos a | ||
75 | s = sin a | ||
76 | |||
77 | fun n r = foldl1' (<>) (map r angles) | ||
78 | where angles = toList $ linspace n (0,1) | ||