Add remaining haskell book exercises
This commit is contained in:
46
Haskell-book/28/Bench/app/Main.hs
Normal file
46
Haskell-book/28/Bench/app/Main.hs
Normal file
@@ -0,0 +1,46 @@
|
||||
module Main where
|
||||
|
||||
import Criterion.Main
|
||||
import qualified Data.Map as M
|
||||
import qualified Data.Set as S
|
||||
import qualified Data.Vector as V
|
||||
import qualified Data.Vector.Unboxed as U
|
||||
|
||||
genList :: Int -> [(String, Int)]
|
||||
genList n = go n []
|
||||
where go 0 xs = ("0", 0) : xs
|
||||
go n' xs = go (n' - 1) ((show n', n') : xs)
|
||||
|
||||
pairList :: [(String, Int)]
|
||||
pairList = genList 9001
|
||||
|
||||
testMap :: M.Map String Int
|
||||
testMap = M.fromList pairList
|
||||
|
||||
testSet :: S.Set String
|
||||
testSet = S.fromList $ fmap fst pairList
|
||||
|
||||
slice :: Int -> Int -> [a] -> [a]
|
||||
slice from len xs = take len (drop from xs)
|
||||
|
||||
boxed :: V.Vector Int
|
||||
boxed = V.fromList [1..1000]
|
||||
|
||||
unboxed :: U.Vector Int
|
||||
unboxed = U.fromList [1..1000]
|
||||
|
||||
main :: IO ()
|
||||
main = defaultMain
|
||||
[ bench "slicing unboxed vector" $
|
||||
whnf (U.head . U.slice 100 900) unboxed
|
||||
, bench "slicing boxed vector" $
|
||||
whnf (V.head . V.slice 100 900) boxed
|
||||
, bench "lookup one thing, set" $
|
||||
whnf (S.member "doesntExist") testSet
|
||||
, bench "insert one thing, set" $
|
||||
whnf (S.insert "doesntExist" ) S.empty
|
||||
, bench "lookup one thing, map" $
|
||||
whnf (M.lookup "doesntExist") testMap
|
||||
, bench "insert one thing, map" $
|
||||
whnf (M.insert ("doesntExist", 0)) M.empty
|
||||
]
|
||||
Reference in New Issue
Block a user