papa-0.7.0: bench/Main.hs
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
{- HLINT ignore "Avoid restricted function" -}
-- Reason: these benchmark Papa's total maximum and div, whose names HLint
-- restricts whatever module they come from.
-- | Benchmarks of the functions that Papa defines, against the base
-- functions they replace where there is one.
module Main (
main,
) where
import qualified BenchVector
import Criterion.Main (bench, bgroup, defaultMain, nf, whnf)
import qualified Data.Either as E
import qualified Data.Map.Strict as M
import Nub (nubBenchmarks)
import Papa
import qualified Papa.Containers.Data.Map as Map
import Text (textBenchmarks)
import Totality (totality)
import qualified Prelude as P
ints :: [Int]
ints =
[1 .. 100000]
pairs :: [(Int, Int)]
pairs =
fmap (\n -> (n, n)) ints
bigMap :: Map.Map Int Int
bigMap =
Map.fromList pairs
eithers :: [Either Int Int]
eithers =
fmap (\n -> bool (Left n) (Right n) (even n)) ints
main :: IO ()
main =
defaultMain
( [ bgroup
"list"
[ bench "maximumOf folded (list)" (whnf (maximumOf folded) ints)
, bench "maximum (Foldable1, NonEmpty)" (whnf maximum (0 :| ints))
, bench "Papa.lookup (last key)" (whnf (lookup 100000) pairs)
, bench "(++) right-nested (10 x 10000)" (nf (\xs -> xs ++ xs ++ xs ++ xs ++ xs ++ xs ++ xs ++ xs ++ xs ++ xs) (take 10000 ints))
, bench "(!?) last index" (whnf (ints !?) 99999)
, bench "Papa.partitionEithers" (nf partitionEithers eithers)
, bench "Data.Either.partitionEithers" (nf E.partitionEithers eithers)
]
, bgroup
"maybe"
[ bench "whenJust (sum over 100000)" (whnf (foldl' (\acc n -> acc + whenJust (\x -> bool Nothing (Just x) (even x)) (* 2) n) 0) ints)
, bench "maybe (sum over 100000)" (whnf (foldl' (\acc n -> acc + maybe n (* 2) (bool Nothing (Just n) (even n))) 0) ints)
, bench "whenJustM Maybe (sum over 100000)" (whnf (foldl' (\acc n -> acc + fromMaybe 0 (whenJustM (\x -> Just (bool Nothing (Just x) (even x))) (Just . (* 2)) n)) 0) ints)
]
, bgroup
"containers"
[ bench "Papa Map.elemAt (sum over 1000)" (whnf (foldl' (\acc i -> acc + maybe 0 snd (Map.elemAt i bigMap)) 0) (take 1000 ints))
, bench "Data.Map elemAt (sum over 1000)" (whnf (foldl' (\acc i -> acc + snd (M.elemAt i bigMap)) 0) (take 1000 ints))
, bench "Papa Map.fromDistinctAscList (100000)" (whnf (maybe 0 Map.size . Map.fromDistinctAscList) pairs)
, bench "Data.Map fromDistinctAscList (100000)" (whnf (M.size . M.fromDistinctAscList) pairs)
, bench "Data.Map fromList (100000, sorted)" (whnf (M.size . M.fromList) pairs)
, bench "Papa Map.! (sum over 1000)" (whnf (foldl' (\acc i -> acc + fromMaybe 0 (bigMap Map.! i)) 0) (take 1000 ints))
, bench "Data.Map ! (sum over 1000)" (whnf (foldl' (\acc i -> acc + (bigMap M.! i)) 0) (take 1000 ints))
]
, bgroup
"fractional"
[ bench "divideNonZero checking each divisor (sum over 100000)" (whnf (foldl' (\acc n -> acc + maybe 0 (divideNonZero 1) (preview nonZero (P.fromIntegral n :: Double))) 0) ints)
, bench "Prelude./ (sum over 100000)" (whnf (foldl' (\acc n -> acc + (P./) 1 (P.fromIntegral n)) (0 :: Double)) ints)
]
, bgroup
"integral"
[ bench "divModNonZero by one NonZero (sum over 100000)" (whnf (\xs -> maybe 0 (\d -> foldl' (\acc n -> acc + fst (divModNonZero n d)) 0 xs) (preview nonZero (7 :: Int))) ints)
, bench "Prelude.divMod (sum over 100000)" (whnf (foldl' (\acc n -> acc + fst (P.divMod n 7)) 0) ints)
]
, totality
, bgroup
"nonEmpty"
[ bench "the head" (whnf (the head) (0 :| ints))
, bench "the last" (whnf (the last) (0 :| ints))
, bench "set last" (nf (set last 1) (0 :| ints))
, bench "the init" (nf (the init) (0 :| ints))
]
, bgroup "text and bytestring" textBenchmarks
]
<> nubBenchmarks
<> BenchVector.benchmarks
)