papa-0.7.0: bench/BenchVector.hs
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
{- HLINT ignore "Avoid restricted function" -}
-- Reason: these benchmark Papa's total head and maximum of vectors against
-- vector's partial ones, whose names HLint restricts whatever module they
-- come from.
-- | Benchmarks of the total vector and hash map functions against the
-- partial ones of vector and unordered-containers, and of lens's 'ix' and
-- 'at' against indexing.
module BenchVector (
benchmarks,
) where
import Criterion.Main (Benchmark, bench, bgroup, nf, whnf)
import qualified Data.HashMap.Strict as OHM
import qualified Data.Vector as OV
import qualified Data.Vector.Unboxed as OU
import Papa
import qualified Papa.UnorderedContainers.Data.HashMap as HashMap
import qualified Papa.Vector.Data.Vector as PV
import qualified Papa.Vector.Data.Vector.Unboxed as PU
size :: Int
size =
100000
boxed :: PV.Vector Int
boxed =
PV.enumFromN 0 size
unboxed :: PU.Vector Int
unboxed =
PU.enumFromN 0 size
someIndices :: [Int]
someIndices =
PV.toList (PV.enumFromStepN 0 7 1000)
updates :: [(Int, Int)]
updates =
fmap (\i -> (i, i)) someIndices
hashMap :: HashMap Int Int
hashMap =
HashMap.fromList (fmap (\i -> (i, i)) (PV.toList boxed))
-- | The benchmarks of the vector and hash map modules.
--
-- A total maximum that returns its 'Maybe' to the benchmark, unconsumed,
-- runs some 20% (boxed) to 45% (unboxed) slower than vector's: GHC allocates
-- the 'Just' and the boxed element in the loop's exit, and so checks the heap
-- on every iteration of the loop (as for @Just $! Data.Vector.maximum v@,
-- written by hand). A use that consumes the 'Maybe' in place
-- (@fromMaybe 0 . maximum@) runs at vector's speed.
benchmarks :: [Benchmark]
benchmarks =
[ bgroup
"vector"
[ bench "Papa V.maximum (100000)" (nf PV.maximum boxed)
, bench "Data.Vector maximum (100000)" (nf OV.maximum boxed)
, bench "Papa U.maximum (100000, unboxed)" (nf PU.maximum unboxed)
, bench "Data.Vector.Unboxed maximum (100000)" (nf OU.maximum unboxed)
, bench "Papa V.maximum, consumed (100000)" (nf (fromMaybe 0 . PV.maximum) boxed)
, bench "Papa U.maximum, consumed (100000, unboxed)" (nf (fromMaybe 0 . PU.maximum) unboxed)
, bench "Papa V.maximumBy compare (100000)" (nf (PV.maximumBy compare) boxed)
, bench "Data.Vector maximumBy compare (100000)" (nf (OV.maximumBy compare) boxed)
, bench "Papa V.foldl1' (100000)" (nf (PV.foldl1' (+)) boxed)
, bench "Data.Vector foldl1' (100000)" (nf (OV.foldl1' (+)) boxed)
, bench "Papa V.// (1000 updates)" (nf (PV.// updates) boxed)
, bench "Data.Vector // (1000 updates)" (nf (OV.// updates) boxed)
, bench "Papa V.backpermute (1000)" (nf (PV.backpermute boxed) (PV.fromList someIndices))
, bench "Data.Vector backpermute (1000)" (nf (OV.backpermute boxed) (OV.fromList someIndices))
, bench "Papa V.slice (1000 slices)" (whnf (foldl' (\acc i -> acc + maybe 0 PV.length (PV.slice i 10 boxed)) 0) someIndices)
, bench "Data.Vector slice (1000 slices)" (whnf (foldl' (\acc i -> acc + OV.length (OV.slice i 10 boxed)) 0) someIndices)
, bench "Papa V.head (1000 tails)" (whnf (foldl' (\acc i -> acc + fromMaybe 0 (PV.head (PV.drop i boxed))) 0) someIndices)
, bench "Data.Vector head (1000 tails)" (whnf (foldl' (\acc i -> acc + OV.head (OV.drop i boxed)) 0) someIndices)
, bench "lens ^? ix (sum over 1000)" (whnf (foldl' (\acc i -> acc + fromMaybe 0 (boxed ^? ix i)) 0) someIndices)
, bench "Data.Vector !? (sum over 1000)" (whnf (foldl' (\acc i -> acc + fromMaybe 0 (boxed OV.!? i)) 0) someIndices)
, bench "Data.Vector ! (sum over 1000)" (whnf (foldl' (\acc i -> acc + (boxed OV.! i)) 0) someIndices)
, bench "lens ^? ix (unboxed, sum over 1000)" (whnf (foldl' (\acc i -> acc + fromMaybe 0 (unboxed ^? ix i)) 0) someIndices)
, bench "Data.Vector.Unboxed ! (sum over 1000)" (whnf (foldl' (\acc i -> acc + (unboxed OU.! i)) 0) someIndices)
, bench "lens ix .~ (1 element)" (nf (\v -> v & ix 500 .~ 0) boxed)
, bench "Papa V.// (1 element)" (nf (PV.// [(500, 0)]) boxed)
]
, bgroup
"unordered-containers"
[ bench "lens the at (sum over 1000)" (whnf (foldl' (\acc i -> acc + fromMaybe 0 (the (at i) hashMap)) 0) someIndices)
, bench "lens ^? ix (sum over 1000)" (whnf (foldl' (\acc i -> acc + fromMaybe 0 (hashMap ^? ix i)) 0) someIndices)
, bench "Data.HashMap.Strict ! (sum over 1000)" (whnf (foldl' (\acc i -> acc + (hashMap OHM.! i)) 0) someIndices)
, bench "lens at ?~ (insert)" (whnf (\m -> HashMap.size (m & at size ?~ 0)) hashMap)
, bench "Data.HashMap.Strict insert" (whnf (HashMap.size . HashMap.insert size 0) hashMap)
]
]