Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. {-# LANGUAGE BangPatterns #-}
  2. {-# LANGUAGE ScopedTypeVariables #-}
  3. {-# LANGUAGE UnboxedTuples #-}
  4.  
  5. module Main where
  6.  
  7. -- criterion
  8. import qualified Criterion.Main as Criterion
  9. ( bench, bgroup, defaultMain, nf )
  10. import Data.Foldable
  11. import Data.STRef
  12. import Control.Monad.ST
  13. import Data.STRef.Unboxed
  14.  
  15. testList :: [Int]
  16. testList = [0..10^6]
  17.  
  18. sum_recurs :: [Int] -> Int
  19. sum_recurs = go 0
  20. where
  21. go :: Int -> [Int] -> Int
  22. go acc [] = acc
  23. go acc (n : ns) = go (acc + n) ns
  24.  
  25.  
  26. sum_boxed_ref :: [Int] -> Int
  27. sum_boxed_ref = go
  28. where
  29. go :: [Int] -> Int
  30. go ns = runST $ do
  31. acc <- newSTRef 0
  32. for_ ns $ \n ->
  33. modifySTRef' acc (+ n)
  34. readSTRef acc
  35.  
  36. sum_unboxed_ref :: [Int] -> Int
  37. sum_unboxed_ref = go
  38. where
  39. go :: [Int] -> Int
  40. go ns = runST $ do
  41. acc <- newSTRefU 0
  42. for_ ns $ \n ->
  43. modifySTRefU acc (+ n)
  44. readSTRefU acc
  45.  
  46. main :: IO ()
  47. main = Criterion.defaultMain
  48. [ Criterion.bgroup "STRefs"
  49. [ Criterion.bench "foldr (+) 0" $ Criterion.nf (foldr (+) 0) testList
  50. , Criterion.bench "sum" $ Criterion.nf sum testList
  51. , Criterion.bench "sum_recurs" $ Criterion.nf sum_recurs testList
  52. , Criterion.bench "sum_boxed_ref" $ Criterion.nf sum_boxed_ref testList
  53. , Criterion.bench "sum_unboxed_ref" $ Criterion.nf sum_unboxed_ref testList
  54. ]
  55. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement