File: Cut.hs

package info (click to toggle)
haskell-text 1.2.0.6-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 900 kB
  • sloc: haskell: 9,299; ansic: 238; python: 87; ruby: 84; sh: 49; makefile: 29
file content (98 lines) | stat: -rw-r--r-- 3,075 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
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
-- | Cut into a file, selecting certain columns (e.g. columns 10 to 40)
--
-- Tested in this benchmark:
--
-- * Reading the file
--
-- * Splitting into lines
--
-- * Taking a number of characters from the lines
--
-- * Joining the lines
--
-- * Writing back to a handle
--
module Benchmarks.Programs.Cut
    ( benchmark
    ) where

import Criterion (Benchmark, bgroup, bench, whnfIO)
import System.IO (Handle, hPutStr)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BC
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Lazy.Char8 as BLC
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.IO as T
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as TL
import qualified Data.Text.Lazy.IO as TL

benchmark :: FilePath -> Handle -> Int -> Int -> IO Benchmark
benchmark p sink from to = return $ bgroup "Cut"
    [ bench' "String" string
    , bench' "ByteString" byteString
    , bench' "LazyByteString" lazyByteString
    , bench' "Text" text
    , bench' "LazyText" lazyText
    , bench' "TextByteString" textByteString
    , bench' "LazyTextByteString" lazyTextByteString
    ]
  where
    bench' n s = bench n $ whnfIO (s p sink from to)

string :: FilePath -> Handle -> Int -> Int -> IO ()
string fp sink from to = do
    s <- readFile fp
    hPutStr sink $ cut s
  where
    cut = unlines . map (take (to - from) . drop from) . lines

byteString :: FilePath -> Handle -> Int -> Int -> IO ()
byteString fp sink from to = do
    bs <- B.readFile fp
    B.hPutStr sink $ cut bs
  where
    cut = BC.unlines . map (B.take (to - from) . B.drop from) . BC.lines

lazyByteString :: FilePath -> Handle -> Int -> Int -> IO ()
lazyByteString fp sink from to = do
    bs <- BL.readFile fp
    BL.hPutStr sink $ cut bs
  where
    cut = BLC.unlines . map (BL.take (to' - from') . BL.drop from') . BLC.lines
    from' = fromIntegral from
    to' = fromIntegral to

text :: FilePath -> Handle -> Int -> Int -> IO ()
text fp sink from to = do
    t <- T.readFile fp
    T.hPutStr sink $ cut t
  where
    cut = T.unlines . map (T.take (to - from) . T.drop from) . T.lines

lazyText :: FilePath -> Handle -> Int -> Int -> IO ()
lazyText fp sink from to = do
    t <- TL.readFile fp
    TL.hPutStr sink $ cut t
  where
    cut = TL.unlines . map (TL.take (to' - from') . TL.drop from') . TL.lines
    from' = fromIntegral from
    to' = fromIntegral to

textByteString :: FilePath -> Handle -> Int -> Int -> IO ()
textByteString fp sink from to = do
    t <- T.decodeUtf8 `fmap` B.readFile fp
    B.hPutStr sink $ T.encodeUtf8 $ cut t
  where
    cut = T.unlines . map (T.take (to - from) . T.drop from) . T.lines

lazyTextByteString :: FilePath -> Handle -> Int -> Int -> IO ()
lazyTextByteString fp sink from to = do
    t <- TL.decodeUtf8 `fmap` BL.readFile fp
    BL.hPutStr sink $ TL.encodeUtf8 $ cut t
  where
    cut = TL.unlines . map (TL.take (to' - from') . TL.drop from') . TL.lines
    from' = fromIntegral from
    to' = fromIntegral to