summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/benchmarks.hs50
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
6import Numeric.LinearAlgebra 7import Numeric.LinearAlgebra
7import System.Time 8import System.Time
8import System.CPUTime 9import System.CPUTime
9import Text.Printf 10import Text.Printf
11import Data.List(foldl1')
10 12
11 13
12time act = do 14time 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
18time' act = do 20time' act = do
19 t0 <- getClockTime 21 t0 <- getClockTime
@@ -23,16 +25,18 @@ time' act = do
23 25
24-------------------------------------------------------------------------------- 26--------------------------------------------------------------------------------
25 27
26main = sequence_ [bench1] 28main = sequence_ [bench1,bench2]
29
30w :: Vector Double
31w = constant 1 30000000
27 32
28bench1 = do 33bench1 = 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
35sumV f n = f (constant (1::Double) n)
36 40
37sumVB v = constant 1 (dim v) <.> v 41sumVB 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
52bench2 = 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
62rot' :: Double -> Matrix Double
63rot' 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
70rot :: Double -> Matrix Double
71rot a = (3><3) [ c,0,s
72 , 0,1,0
73 ,-s,0,c ]
74 where c = cos a
75 s = sin a
76
77fun n r = foldl1' (<>) (map r angles)
78 where angles = toList $ linspace n (0,1)