streamly/examples/FileIO.hs
Harendra Kumar fc6d77a025 Fix a performance issue with File.read
We were using bracket on a loop which iterates very frequently. Instead use it
on an outer loop which is much leass frequent.

Also add FileIO.hs to test drive the performance of FileIO APIs.
2019-05-31 09:49:32 +05:30

53 lines
1.7 KiB
Haskell

import qualified Streamly.Prelude as S
import qualified Streamly.Fold as FL
import qualified Streamly.Mem.Array as A
import qualified Streamly.FileSystem.File as File
import Data.Char (ord)
import System.Environment (getArgs)
cat :: FilePath -> IO ()
cat src = File.writeArrays "/dev/stdout" $ File.readArraysUpto (256*1024) src
cp :: FilePath -> FilePath -> IO ()
cp src dst = File.writeArrays dst $ File.readArraysUpto (256*1024) src
ord' :: Num a => Char -> a
ord' = (fromIntegral . ord)
wcl :: FilePath -> IO ()
wcl src = print =<< (S.length
$ FL.splitSuffixBy (== ord' '\n') FL.drain
$ File.read src)
grepc :: String -> FilePath -> IO ()
grepc pat src = print . (subtract 1) =<< (S.length
$ FL.splitOn (A.fromList (map ord' pat)) FL.drain
$ File.read src)
avgll :: FilePath -> IO ()
avgll src = print =<< (FL.foldl' avg
$ FL.splitSuffixBy (== ord' '\n') FL.length
$ File.read src)
where avg = (/) <$> toDouble FL.sum <*> toDouble FL.length
toDouble = fmap (fromIntegral :: Int -> Double)
llhisto :: FilePath -> IO ()
llhisto src = print =<< (FL.foldl' (FL.classify FL.length)
$ S.map bucket
$ FL.splitSuffixBy (== ord' '\n') FL.length
$ File.read src)
where
bucket n = let i = n `div` 10 in if i > 9 then (9,n) else (i,n)
main :: IO ()
main = do
src <- fmap head getArgs
putStrLn "cat" >> cat src -- Unix cat program
putStr "wcl " >> wcl src -- Unix wc -l program
putStr "grepc " >> grepc "aaaa" src -- Unix grep -c program
putStr "avgll " >> avgll src -- get average line length
putStr "llhisto " >> llhisto src -- get line length histogram
putStr "cp " >> cp src "dst-xyz.txt" -- Unix cp program