-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.purs
74 lines (67 loc) · 1.95 KB
/
Main.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
module Main where
import Prelude
import Benchotron.Core (Benchmark, benchFn, mkBenchmark)
import Benchotron.UI.Console (runSuite)
import Control.Monad.RWS as Trs
import Control.Monad.Rec.Class (Step(..), tailRecM)
import Control.Monad.State.Trans (get, put)
import Control.Monad.Trampoline (Trampoline, runTrampoline)
import Data.Array ((..))
import Effect (Effect)
import Uncurried.State as Uncurried
countToN :: Benchmark
countToN = mkBenchmark
{ slug: "countToN"
, title: "Count-To-N"
, sizes: (1 .. 10) <#> (1000 * _)
, sizeInterpretation: "Limit"
, inputsPerSize: 100
, gen: pure
, functions:
[ benchFn "naive-transformers" $ \n -> runTrampoline $ Trs.runRWST (program' n) unit 0
, benchFn "tailrecm-transformers" $ \n -> runTrampoline $ Trs.runRWST (programSafe' n) unit 0
, benchFn "naive-uncurried-transformers" $ \n -> Uncurried.runState 0 (program n)
, benchFn "tailrecm-uncurried-transformers" $ \n -> Uncurried.runState 0 (programSafe n)
]
}
where
program :: Int -> Uncurried.State Int Unit
program limit = go unit
where
go _ = do
current <- get
unless (current == limit) do
put (current + 1)
go unit
program' :: Int -> Trs.RWST Unit Unit Int Trampoline Unit
program' limit = go unit
where
go _ = do
current <- get
unless (current == limit) do
put (current + 1)
go unit
programSafe :: Int -> Uncurried.State Int Unit
programSafe limit = tailRecM go unit
where
go _ = do
current <- get
if current == limit then do
pure $ Done unit
else do
put (current + 1)
pure $ Loop unit
programSafe' :: Int -> Trs.RWST Unit Unit Int Trampoline Unit
programSafe' limit = tailRecM go unit
where
go _ = do
current <- get
if current == limit then do
pure $ Done unit
else do
put (current + 1)
pure $ Loop unit
main :: Effect Unit
main = runSuite
[ countToN
]