forked from acowley/Frames
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsuranceBench.hs
More file actions
78 lines (69 loc) · 2.94 KB
/
InsuranceBench.hs
File metadata and controls
78 lines (69 loc) · 2.94 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
{-# LANGUAGE BangPatterns,
DataKinds,
FlexibleContexts,
TemplateHaskell #-}
import Criterion.Main
import qualified Data.Foldable as F
import Data.Functor.Identity
import Frames
import qualified Pipes as P
import qualified Pipes.Prelude as P
tableTypes "Ins" "data/FL2.csv"
type TinyIns = Record [PolicyID, PointLatitude, PointLongitude]
tblP :: P.Producer Ins IO ()
tblP = readTable "data/FL2.csv"
-- Strict pair
data P a = P !a !a
-- | Perform two consecutive folds of streamed-in data.
pipeBench :: IO (P Double)
pipeBench = do (n,sumLat) <-
P.fold (\ !(!i, !s) r -> (i+1, s+rget pointLatitude r))
(0::Int,0)
id
tbl
sumLong <- P.fold (\s r -> (s + rget pointLongitude r)) 0 id tbl
return $! P (sumLat / fromIntegral n) (sumLong / fromIntegral n)
where tbl = P.for tblP (P.yield . rcast) :: P.Producer TinyIns IO ()
-- | Perform two consecutive folds after first streaming all data into
-- an in-memory representation.
pipeBenchInCore :: IO (P Double)
pipeBenchInCore =
do tbl <- inCore tblP :: IO (P.Producer Ins Identity ())
let Identity (n,sumLat) =
P.fold (\ !(!i, !s) r -> (i+1, s+rget pointLatitude r))
(0::Int,0)
id
tbl
Identity sumLong =
P.fold (\s r -> (s + rget pointLongitude r)) 0 id tbl
return $! P (sumLat / fromIntegral n) (sumLong / fromIntegral n)
-- | Perform two consecutive folds after first projecting a subset of
-- fields while streaming data into an in-memory representation.
pipeBenchInCore' :: IO (P Double)
pipeBenchInCore' =
do tbl <- inCore $ P.for tblP (P.yield . rcast)
:: IO (P.Producer TinyIns Identity ())
let Identity (n,sumLat) =
P.fold (\ !(!i, !s) r -> (i+1, s+rget pointLatitude r))
(0::Int,0)
id
tbl
Identity sumLong =
P.fold (\s r -> (s + rget pointLongitude r)) 0 id tbl
return $! P (sumLat / fromIntegral n) (sumLong / fromIntegral n)
-- | Perform two consecutive folds after projecting a subset of an
-- in-memory reprsentation.
pipeBenchAoS :: IO (P Double)
pipeBenchAoS = do tbl <- inCoreAoS' rcast tblP :: IO (Frame TinyIns)
let (n,sumLat) =
F.foldl' (\ !(!i,!s) r -> (i+1, s+rget pointLatitude r))
(0::Int,0)
tbl
sumLong =
F.foldl' (\ !s r -> (s + rget pointLongitude r)) 0 tbl
return $! P (sumLat / fromIntegral n) (sumLong / fromIntegral n)
main :: IO ()
main = defaultMain [ bench "pipes" $ whnfIO pipeBench
, bench "pipes in-core" $ whnfIO pipeBenchInCore
, bench "pipes in-core subset" $ whnfIO pipeBenchInCore'
, bench "pipes AoS" $ whnfIO pipeBenchAoS ]