packages feed

lynceus-0.0.1.0: bench/Main.hs

module Main (main) where

import Data.Foldable (for_)
import Data.IORef (IORef, modifyIORef', newIORef, readIORef)
import Data.List (sort)

import Lynceus

main :: IO ()
main =
  defaultMain
    "bench/bench.record"
    [ group
        "sort"
        [ bench "ascending" (nf sort [1 .. 100000 :: Int]),
          bench "descending" (nf sort [100000, 99999 .. 1 :: Int])
        ],
      group
        "list"
        [ bench "reverse" (whnf reverse [1 .. 100000 :: Int]),
          bench "accumulate" (withSetup (newIORef 0) (nfIO . accumulate))
        ]
    ]

-- | Add the numbers up to 100000 to a reference and read it.
accumulate :: IORef Int -> IO Int
accumulate ref = do
  for_ [1 .. 100000] $ \i -> modifyIORef' ref (+ i)
  readIORef ref