Skip to content

Instantly share code, notes, and snippets.

@kuribas
Last active August 7, 2017 19:34
Show Gist options
  • Select an option

  • Save kuribas/c61f16d902a6586cf488891007067921 to your computer and use it in GitHub Desktop.

Select an option

Save kuribas/c61f16d902a6586cf488891007067921 to your computer and use it in GitHub Desktop.
reddit towers benchmark
import Criterion.Main
import System.Random
import Data.List
import qualified Data.Vector.Unboxed as U
rainfall1 :: [Int] -> Int
rainfall1 xs = sum (zipWith (-) mins xs)
where
mins = zipWith min maxl maxr
maxl = scanl1 max xs
maxr = scanr1 max xs
rainfall2 :: U.Vector Int -> Int
rainfall2 xs = U.sum (U.zipWith (-) mins xs)
where
mins = U.zipWith min maxl maxr
maxl = U.scanl1' max xs
maxr = U.scanr1' max xs
-- Our benchmark harness.
main = do
g <- getStdGen
let hs = take 10000000 (randomRs (0, 200) g :: [Int])
defaultMain [
bgroup "rainfall" [
bench "rainfall1" $ whnf rainfall1 hs,
bench "rainfall2" $ whnf rainfall2 (U.fromList hs)
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment