From 98329e0a3dd4f78b5d815ac3896272ec70904901 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Thu, 11 Dec 2025 10:28:11 +0100 Subject: Add remaining haskell book exercises --- Haskell-book/28/Bench/app/Main.hs | 46 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Haskell-book/28/Bench/app/Main.hs (limited to 'Haskell-book/28/Bench/app/Main.hs') diff --git a/Haskell-book/28/Bench/app/Main.hs b/Haskell-book/28/Bench/app/Main.hs new file mode 100644 index 0000000..c3c602f --- /dev/null +++ b/Haskell-book/28/Bench/app/Main.hs @@ -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 + ] -- cgit v1.2.3