diff options
| author | Eugen Wissner <belka@caraus.de> | 2025-12-11 10:28:11 +0100 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2025-12-11 10:28:11 +0100 |
| commit | 98329e0a3dd4f78b5d815ac3896272ec70904901 (patch) | |
| tree | 80f9c56cfe2ac20232358f236d32e84bd683be1b /Haskell-book/28/Bench/app/Main.hs | |
| parent | 3624c712d72d246f21d4e710cec7c11e052e0326 (diff) | |
| download | book-exercises-98329e0a3dd4f78b5d815ac3896272ec70904901.tar.gz | |
Add remaining haskell book exercises
Diffstat (limited to 'Haskell-book/28/Bench/app/Main.hs')
| -rw-r--r-- | Haskell-book/28/Bench/app/Main.hs | 46 |
1 files changed, 46 insertions, 0 deletions
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 + ] |
