Skip to content

Instantly share code, notes, and snippets.

@AndreasPK
Last active February 18, 2023 23:29
Show Gist options
  • Save AndreasPK/2e2cec5b8845c820afdb0d8a6a9723af to your computer and use it in GitHub Desktop.
Save AndreasPK/2e2cec5b8845c820afdb0d8a6a9723af to your computer and use it in GitHub Desktop.
Unknown call overhead.
module Lib where
import Data.Vector as V
type LookupCached a = Int -> a
mkCache :: [a] -> LookupCached a
mkCache xs =
let !cache = V.fromList xs
in V.unsafeIndex cache
type CachedList1 a = Vector a
mkCache2 :: [a] -> CachedList1 a
mkCache2 xs = V.fromList xs
lookupCache :: CachedList1 a -> Int -> a
lookupCache !cache !i = V.unsafeIndex cache i
{-# OPTIONS_GHC -O2 -fproc-alignment=64 #-}
import Criterion.Main
import Lib
index = 1 :: Int
-- Our benchmark harness.
main = defaultMain [
bgroup "vec-cache"
[ env
(pure $ mkCache $ [0..1000::Int] )
(\cache -> bench "higher-order" $ whnf cache index)
, env
(pure $ mkCache2 $ [0..1000::Int] )
(\cache -> bench "defunctionalized" $ whnf (lookupCache cache) index)
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment