File: folds.hs

package info (click to toggle)
haskell-lens 4.18.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,016 kB
  • sloc: haskell: 16,792; sh: 15; makefile: 14; ansic: 8
file content (110 lines) | stat: -rw-r--r-- 3,017 bytes parent folder | download | duplicates (2)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE BangPatterns #-}

import qualified Data.ByteString as BS
import qualified Data.Foldable as F
import qualified Data.HashMap.Lazy as HM
import qualified Data.Map as M
import qualified Data.Sequence as S
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as U

import Data.Vector.Generic.Lens
import Data.ByteString.Lens

import Control.Lens
import Criterion.Main
import Criterion.Types

main :: IO ()
main = defaultMainWith config
  [
    bgroup "vector"
    [ bgroup "toList"
      [ bench "native" $ nf V.toList v
      , bench "each"   $ nf (toListOf each) v
      ]
    , bgroup "itoList"
      [ bench "native"     $ nf (V.toList . V.indexed) v
      , bench "itraversed" $ nf (itoListOf itraversed) v
      ]
    ]
  , bgroup "unboxed-vector"
    [ bgroup "toList"
      [ bench "native" $ nf U.toList u
      , bench "each"   $ nf (toListOf each) u
      ]
    , bgroup "itoList"
      [ bench "native"     $ nf (U.toList . U.indexed) u
      , bench "vTraverse" $ nf (itoListOf vectorTraverse) u
      ]
    ]
  , bgroup "sequence"
    [ bgroup "toList"
      [ bench "native" $ nf F.toList s
      , bench "each"   $ nf (toListOf each) s
      ]
    , bgroup "itoList"
      [ bench "native"     $ nf (F.toList . S.mapWithIndex (,)) s
      , bench "itraversed" $ nf (itoListOf itraversed) s
      ]
    ]
  , bgroup "bytestring"
    [ bgroup "toList"
      [ bench "native" $ nf BS.unpack b
      , bench "bytes"  $ nf (toListOf bytes) b
      , bench "each"   $ nf (toListOf each) b
      ]
    , bgroup "itoList"
      [ bench "native" $ nf (zip [(0::Int)..] . BS.unpack) b
      , bench "bytes"  $ nf (itoListOf bytes) b
      ]
    ]
  , bgroup "list"
    [ bgroup "toList"
      [ bench "native" $ nf F.toList l
      , bench "each"   $ nf (toListOf each) l
      ]
    , bgroup "itoList"
      [ bench "native"     $ nf (zip [(0::Int)..]) l
      , bench "itraversed" $ nf (itoListOf itraversed) l
      ]
    ]
  , bgroup "map"
    [ bgroup "toList"
      [ bench "native" $ nf F.toList m
      , bench "each"   $ nf itoList m
      ]
    , bgroup "itoList"
      [ bench "native"     $ nf (zip [(0::Int)..] . F.toList) m
      , bench "itraversed" $ nf (itoListOf itraversed) m
      ]
    ]
  , bgroup "hash map"
    [ bgroup "toList"
      [ bench "native" $ nf HM.keys h
      , bench "each"   $ nf (toListOf each) h
      ]
    , bgroup "itoList"
      [ bench "native"     $ nf HM.toList h
      , bench "itoList"    $ nf itoList h
      , bench "itraversed" $ nf (itoListOf itraversed) h
      ]
    , bgroup "sum"
      [ bench "native" $ nf (sum . id . F.toList) h
      , bench "each"   $ nf (sumOf each) h
      ]
    ]
  ]
  where
    config = defaultConfig { timeLimit = 1 }
    l = [0..10000] :: [Int]
    b = BS.pack $ map fromIntegral l
    h = HM.fromList $ zip l l
    m = M.fromList $ zip l l
    s = S.fromList l
    u = U.fromList l
    v = V.fromList l