File: Allocations.hs

package info (click to toggle)
haskell-store 0.7.18-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 284 kB
  • sloc: haskell: 2,663; makefile: 9
file content (60 lines) | stat: -rw-r--r-- 2,423 bytes parent folder | download | duplicates (4)
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
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE BangPatterns #-}

-- | Weigh Store's operations.

module Main where

import           Control.DeepSeq
import qualified Data.IntMap.Strict as IntMap
import qualified Data.IntSet as IntSet
import qualified Data.Serialize as Cereal
import qualified Data.Set as Set
import qualified Data.Map.Strict as Map
import qualified Data.Store as Store
import qualified Data.Vector as Boxed
import qualified Data.Vector.Serialize ()
import qualified Data.Vector.Storable as Storable
import           Text.Printf
import           Weigh

-- | Main entry point.
main :: IO ()
main =
  mainWith weighing

-- | Weigh weighing with Store vs Cereal.
weighing :: Weigh ()
weighing =
  do fortype "[Int]" (\n -> replicate n 0 :: [Int])
     fortype "Boxed Vector Int" (\n -> Boxed.replicate n 0 :: Boxed.Vector Int)
     fortype "Storable Vector Int"
             (\n -> Storable.replicate n 0 :: Storable.Vector Int)
     fortype "Set Int" (Set.fromDistinctAscList . ints)
     fortype "IntSet" (IntSet.fromDistinctAscList . ints)
     fortype "Map Int Int" (Map.fromDistinctAscList . intpairs)
     fortype "IntMap Int" (IntMap.fromDistinctAscList . intpairs)
  where fortype label make =
          scale (\(n,nstr) ->
                   do let title :: String -> String
                          title for = printf "%12s %-20s %s" nstr (label :: String) for
                          encodeDecode en de =
                            (return . (`asTypeOf` make n) . de . force . en . make) n
                      action (title "Allocate")
                             (return (make n))
                      action (title "Encode: Store")
                             (return (Store.encode (force (make n))))
                      action (title "Encode: Cereal")
                             (return (Cereal.encode (force (make n))))
                      action (title "Encode/Decode: Store")
                             (encodeDecode Store.encode Store.decodeEx)
                      action (title "Encode/Decode: Cereal")
                             (encodeDecode Cereal.encode (fromRight . Cereal.decode)))
        scale f =
          mapM_ f
                (map (\x -> (x,commas x))
                     [1000000,2000000,10000000])
        ints n = [1..n] :: [Int]
        intpairs = map (\x -> (x, x)) . ints
        fromRight = either (error "Left") id