diff options
author | Alberto Ruiz <aruiz@um.es> | 2008-06-10 08:31:27 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2008-06-10 08:31:27 +0000 |
commit | 53b361e5b9840273ccfd50e5b256002498a97469 (patch) | |
tree | c96ae1a373cd6884821f4b74b9e6cabd5e6f2204 /examples | |
parent | 9cb72979acc7bcd9df2fa8eab05169d9c5ca84f5 (diff) |
added examples/benchmarks.hs
Diffstat (limited to 'examples')
-rw-r--r-- | examples/benchmarks.hs | 44 |
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 | |||
6 | import Numeric.LinearAlgebra | ||
7 | import System.Time | ||
8 | import System.CPUTime | ||
9 | import Text.Printf | ||
10 | |||
11 | |||
12 | time act = do | ||
13 | t0 <- getCPUTime | ||
14 | act | ||
15 | t1 <- getCPUTime | ||
16 | putStrLn $ show ((t1-t0) `div` 10^10) ++ " cs" | ||
17 | |||
18 | time' act = do | ||
19 | t0 <- getClockTime | ||
20 | act | ||
21 | t1 <- getClockTime | ||
22 | putStrLn $ timeDiffToString $ normalizeTimeDiff $ diffClockTimes t1 t0 | ||
23 | |||
24 | -------------------------------------------------------------------------------- | ||
25 | |||
26 | main = sequence_ [bench1] | ||
27 | |||
28 | bench1 = 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 | |||
35 | sumV f n = f (constant (1::Double) n) | ||
36 | |||
37 | sumVB v = constant 1 (dim v) <.> v | ||
38 | |||
39 | sumVH 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)) | ||