summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2008-06-10 08:31:27 +0000
committerAlberto Ruiz <aruiz@um.es>2008-06-10 08:31:27 +0000
commit53b361e5b9840273ccfd50e5b256002498a97469 (patch)
treec96ae1a373cd6884821f4b74b9e6cabd5e6f2204 /examples
parent9cb72979acc7bcd9df2fa8eab05169d9c5ca84f5 (diff)
added examples/benchmarks.hs
Diffstat (limited to 'examples')
-rw-r--r--examples/benchmarks.hs44
1 files changed, 44 insertions, 0 deletions
diff --git a/examples/benchmarks.hs b/examples/benchmarks.hs
new file mode 100644
index 0000000..7fea3ed
--- /dev/null
+++ b/examples/benchmarks.hs
@@ -0,0 +1,44 @@
1{-# OPTIONS -fbang-patterns #-}
2
3-- compile as:
4-- ghc --make -O2 -optc-O2 -fvia-C benchmarks.hs -o benchmarks
5
6import Numeric.LinearAlgebra
7import System.Time
8import System.CPUTime
9import Text.Printf
10
11
12time act = do
13 t0 <- getCPUTime
14 act
15 t1 <- getCPUTime
16 putStrLn $ show ((t1-t0) `div` 10^10) ++ " cs"
17
18time' act = do
19 t0 <- getClockTime
20 act
21 t1 <- getClockTime
22 putStrLn $ timeDiffToString $ normalizeTimeDiff $ diffClockTimes t1 t0
23
24--------------------------------------------------------------------------------
25
26main = sequence_ [bench1]
27
28bench1 = do
29 putStrLn "sum of a vector with 30 million doubles:"
30 time $ printf " BLAS: %.2f: " $ sumV sumVB 30000000
31 time $ printf " Haskell: %.2f: " $ sumV sumVH 30000000
32 time $ printf " BLAS: %.2f: " $ sumV sumVB 30000000
33 time $ printf " Haskell: %.2f: " $ sumV sumVH 30000000
34
35sumV f n = f (constant (1::Double) n)
36
37sumVB v = constant 1 (dim v) <.> v
38
39sumVH v = go (d - 1) 0
40 where
41 d = dim v
42 go :: Int -> Double -> Double
43 go 0 s = s + (v @> 0)
44 go !j !s = go (j - 1) (s + (v @> j))