aboutsummaryrefslogtreecommitdiff
path: root/Haskell-book/28/Bench/app
diff options
context:
space:
mode:
Diffstat (limited to 'Haskell-book/28/Bench/app')
-rw-r--r--Haskell-book/28/Bench/app/Main.hs46
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
+ ]