fork download
  1. {-# LANGUAGE MagicHash, ScopedTypeVariables, UnboxedTuples, BangPatterns, OverlappingInstances, TypeFamilies, OverloadedStrings, NoMonomorphismRestriction, FlexibleInstances, FlexibleContexts, FunctionalDependencies, MultiParamTypeClasses, RankNTypes, DataKinds, GADTs, GeneralizedNewtypeDeriving, TemplateHaskell #-}
  2. module Main
  3. ( main
  4. , foo
  5. , bar
  6. , baz
  7. , fast
  8. , fastST ) where
  9.  
  10. import Criterion
  11. import Criterion.Main
  12. import Criterion.Types
  13. import Data.IORef
  14. import Data.STRef
  15. import qualified Data.List.Stream as S
  16. import Control.Monad.ST
  17. import Control.Monad.Stream
  18. import Control.Monad.Trans
  19. import Control.Monad.Trans.RWS.Strict
  20. import Control.Monad.Trans.State.Strict
  21. import Control.Monad.Trans.Maybe
  22. import Data.Text (Text)
  23. import qualified Data.Text as T
  24.  
  25. newtype Foo a = Foo { runFoo :: IO a }
  26. newtype Bar a = Bar { unBar :: StateT Int IO a }
  27. newtype Baz a = Baz { unBaz :: RWST Int [Text] Int (StateT Int IO) a }
  28.  
  29. runBar x = (flip evalStateT) 0 . unBar $ x
  30. runBaz x = (flip evalStateT) 0 . liftM fst . (\y -> evalRWST y 0 0) . unBaz $ x
  31.  
  32. n = 10000 :: Int
  33.  
  34. foo :: Int -> Foo Int
  35. foo inp = Foo $ do
  36. x <- newIORef inp
  37. replicateM_ n $ do
  38. modifyIORef x (+1)
  39. readIORef x
  40.  
  41. bar :: Int -> Bar Int
  42. bar inp = Bar $ do
  43. x <- liftIO $ newIORef inp
  44. replicateM_ n $ do
  45. liftIO $ modifyIORef x (\i -> i + 1)
  46. liftIO $ readIORef x
  47.  
  48. baz :: Int -> Baz Int
  49. baz inp = Baz $ do
  50. x <- liftIO $ newIORef inp
  51. replicateM_ n $ do
  52. liftIO $ modifyIORef x (+1)
  53. liftIO $ readIORef x
  54.  
  55. fast :: Int -> Int -> Int
  56. fast 0 acc = acc
  57. fast k acc = fast (k - 1) (acc + 1)
  58.  
  59. fastST :: Int -> Int
  60. fastST inp = runST $ do
  61. x <- newSTRef inp
  62. replicateM_ n $ do
  63. modifySTRef x (\i -> i +1)
  64. readSTRef x
  65.  
  66. main = defaultMain $
  67. [ bench "IO: " $ runFoo $ foo 0
  68. , bench "IO with StateT: " $ runBar $ bar 0
  69. , bench "IO with StateT and RWST: " $ runBaz $ baz 0
  70. , bench "Pure: " $ whnf (\x -> fast x 0) n
  71. , bench "Pure with ST: " $ whnf (\x -> fastST x) n ]
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.hs:23:18:
    Could not find module `Data.Text'
    Perhaps you meant Data.Set (from containers-0.5.0.0)
    Use -v to see a list of the files searched for.
stdout
Standard output is empty