summaryrefslogtreecommitdiff
path: root/examples/benchmarks.hs
blob: 7fea3ed07740159b7d3efac093999526196d350c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
{-# OPTIONS -fbang-patterns #-}

-- compile as:
-- ghc --make -O2 -optc-O2 -fvia-C  benchmarks.hs -o benchmarks

import Numeric.LinearAlgebra
import System.Time
import System.CPUTime
import Text.Printf


time act = do
    t0 <- getCPUTime
    act
    t1 <- getCPUTime
    putStrLn $ show ((t1-t0) `div` 10^10) ++ " cs"

time' act = do
    t0 <- getClockTime
    act
    t1 <- getClockTime
    putStrLn $ timeDiffToString $ normalizeTimeDiff $ diffClockTimes t1 t0

--------------------------------------------------------------------------------

main = sequence_ [bench1]

bench1 = do
    putStrLn "sum of a vector with 30 million doubles:"
    time $ printf "     BLAS: %.2f: " $ sumV sumVB 30000000
    time $ printf "  Haskell: %.2f: " $ sumV sumVH 30000000
    time $ printf "     BLAS: %.2f: " $ sumV sumVB 30000000
    time $ printf "  Haskell: %.2f: " $ sumV sumVH 30000000

sumV f n = f (constant (1::Double) n)

sumVB v = constant 1 (dim v) <.> v

sumVH v = go (d - 1) 0
     where
       d = dim v
       go :: Int -> Double -> Double
       go 0 s = s + (v @> 0)
       go !j !s = go (j - 1) (s + (v @> j))