packages feed

papa-0.7.0: bench/Nub.hs

{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}

{- HLINT ignore "Use nubOrd" -}
-- Reason: this benchmarks nub against nubOrd, which the hint would replace
-- it with.

-- | Benchmarks of Papa's duplicate removal, against "Data.List"'s @nub@.
module Nub (
  nubBenchmarks,
) where

import Criterion.Main (Benchmark, bench, bgroup, nf)
import qualified Data.List as L
import Papa

-- | 2000 elements, 1000 of them distinct.
duplicates :: [Int]
duplicates =
  maybe [] (\d -> fmap (`modNonZero` d) [1 .. 2000]) (preview nonZero (1000 :: Int))

-- | Papa's duplicate removal, against "Data.List"'s @nub@.
nubBenchmarks :: [Benchmark]
nubBenchmarks =
  [ bgroup
      "nub"
      [ bench "Data.List nub" (nf L.nub duplicates)
      , bench "nubOrd" (nf nubOrd duplicates)
      , bench "nubInt" (nf nubInt duplicates)
      ]
  ]