Skip to content

Commit

Permalink
Added Effect.Now version
Browse files Browse the repository at this point in the history
  • Loading branch information
min-nguyen committed Oct 2, 2023
1 parent 3124990 commit aff4628
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 92 deletions.
88 changes: 43 additions & 45 deletions test/Benchmark/ExperimentEffectNow.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
module Test.Benchmark.ExperimentEffectNow

where
module Test.Benchmark.ExperimentEffectNow where

import Prelude

Expand All @@ -14,77 +12,77 @@ import Effect.Now (nowTime)
fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
fib n = fib (n - 1) + fib (n - 2)

diff' :: Time -> Time -> Milliseconds
diff' t2 t1 = diff t2 t1 :: Milliseconds

-- | Benchmarking Fibonacci, where we let-bind its result
benchFibLetBind :: Int -> Effect Int
benchFibLetBind n = do
t1 <- nowTime
let m = fib n -- If `m` is not yet used
t2 <- nowTime
log (show (diff' t2 t1) <> "ms") -- prints 0.0 ms
pure m
t1 <- nowTime
let m = fib n -- If `m` is not yet used
t2 <- nowTime
log (show (diff' t2 t1) <> "ms") -- prints 0.0 ms
pure m

-- | Benchmarking Fibonacci, where we let-bind its result and log it
benchFibLetBindPrint :: Int -> Effect Int
benchFibLetBindPrint n = do
t1 <- nowTime
let m = fib n -- If `m` is subsequently printed
log (show m)
t2 <- nowTime
log (show (diff' t2 t1) <> "ms") -- prints correct ms
pure m
t1 <- nowTime
let m = fib n -- If `m` is subsequently printed
log (show m)
t2 <- nowTime
log (show (diff' t2 t1) <> "ms") -- prints correct ms
pure m

-- | Benchmarking Fibonacci, where we monadically bind its result (using `pure`)
benchFibDoBind :: Int -> Effect Int
benchFibDoBind n = do
t1 <- nowTime
m <- pure $ fib n -- If `fib` is wrapped in `pure`, and the result `m` is not yet used
t2 <- nowTime
log (show (diff' t2 t1) <> "ms") -- prints 0.0 ms
pure m
t1 <- nowTime
m <- pure $ fib n -- If `fib` is wrapped in `pure`, and the result `m` is not yet used
t2 <- nowTime
log (show (diff' t2 t1) <> "ms") -- prints 0.0 ms
pure m

-- | Benchmarking Fibonacci, where we monadically bind its result (using `pure`) and log it
benchFibDoBindPrint :: Int -> Effect Int
benchFibDoBindPrint n = do
t1 <- nowTime
m <- pure $ fib n -- If `fib` is wrapped in `pure`, and the result `m` is subsequently printed
log (show m)
t2 <- nowTime
log (show (diff' t2 t1) <> "ms") -- prints correct ms
pure m
t1 <- nowTime
m <- pure $ fib n -- If `fib` is wrapped in `pure`, and the result `m` is subsequently printed
log (show m)
t2 <- nowTime
log (show (diff' t2 t1) <> "ms") -- prints correct ms
pure m

-- | Fibonacci as an effectful function
fibm :: Int -> Effect Int
fibm 0 = pure 0
fibm 1 = pure 1
fibm n = do
n1 <- fibm (n-1)
n2 <- fibm (n-2)
pure (n1 + n2)
n1 <- fibm (n - 1)
n2 <- fibm (n - 2)
pure (n1 + n2)

-- | Benchmarking the effectful version of Fibonacci, where we monadically bind its result
benchFibmBind :: Int -> Effect Int
benchFibmBind n = do
t1 <- nowTime
m <- fibm n -- If `fib` itself is defined as an effectful computation `fibm`
t2 <- nowTime
log (show (diff t2 t1 :: Milliseconds) <> "ms") -- prints correct ms (without needing the result `m` to be used beforehand)
pure m
t1 <- nowTime
m <- fibm n -- If `fib` itself is defined as an effectful computation `fibm`
t2 <- nowTime
log (show (diff t2 t1 :: Milliseconds) <> "ms") -- prints correct ms (without needing the result `m` to be used beforehand)
pure m

benchFibs :: Effect Unit
benchFibs = do
log ("-- | Benchmarking Fibonacci, where we let-bind its result")
_ <- benchFibLetBind 40
log ("-- | Benchmarking Fibonacci, where we let-bind its result and log it")
_ <- benchFibLetBindPrint 40
log ("-- | Benchmarking Fibonacci, where we monadically bind its result (using `pure`)")
_ <- benchFibDoBind 40
log ("-- | Benchmarking Fibonacci, where we monadically bind its result (using `pure`) and log it")
_ <- benchFibDoBindPrint 40
log ("-- | Benchmarking the effectful version of Fibonacci, where we monadically bind its result")
_ <- benchFibmBind 40
pure unit
log ("-- | Benchmarking Fibonacci, where we let-bind its result")
_ <- benchFibLetBind 35
log ("-- | Benchmarking Fibonacci, where we let-bind its result and log it")
_ <- benchFibLetBindPrint 35
log ("-- | Benchmarking Fibonacci, where we monadically bind its result (using `pure`)")
_ <- benchFibDoBind 35
log ("-- | Benchmarking Fibonacci, where we monadically bind its result (using `pure`) and log it")
_ <- benchFibDoBindPrint 35
log ("-- | Benchmarking the effectful version of Fibonacci, where we monadically bind its result")
_ <- benchFibmBind 35
pure unit
88 changes: 43 additions & 45 deletions test/Benchmark/ExperimentJSDate.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
module Test.Benchmark.JSDate

where
module Test.Benchmark.JSDate where

import Prelude

Expand All @@ -15,74 +13,74 @@ now' = now >>= getMilliseconds
fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
fib n = fib (n - 1) + fib (n - 2)

-- | Benchmarking Fibonacci, where we let-bind its result
benchFibLetBind :: Int -> Effect Int
benchFibLetBind n = do
t1 <- now'
let m = fib n -- If `m` is not yet used
t2 <- now'
log (show (t2 - t1) <> "ms") -- prints 0.0 ms
pure m
t1 <- now'
let m = fib n -- If `m` is not yet used
t2 <- now'
log (show (t2 - t1) <> "ms") -- prints 0.0 ms
pure m

-- | Benchmarking Fibonacci, where we let-bind its result and log it
benchFibLetBindPrint :: Int -> Effect Int
benchFibLetBindPrint n = do
t1 <- now'
let m = fib n -- If `m` is subsequently printed
log (show m)
t2 <- now'
log (show (t2 - t1) <> "ms") -- prints correct ms
pure m
t1 <- now'
let m = fib n -- If `m` is subsequently printed
log (show m)
t2 <- now'
log (show (t2 - t1) <> "ms") -- prints correct ms
pure m

-- | Benchmarking Fibonacci, where we monadically bind its result (using `pure`)
benchFibDoBind :: Int -> Effect Int
benchFibDoBind n = do
t1 <- now'
m <- pure $ fib n -- If `fib` is wrapped in `pure`, and the result `m` is not yet used
t2 <- now'
log (show (t2 - t1) <> "ms") -- prints 0.0 ms
pure m
t1 <- now'
m <- pure $ fib n -- If `fib` is wrapped in `pure`, and the result `m` is not yet used
t2 <- now'
log (show (t2 - t1) <> "ms") -- prints 0.0 ms
pure m

-- | Benchmarking Fibonacci, where we monadically bind its result (using `pure`) and log it
benchFibDoBindPrint :: Int -> Effect Int
benchFibDoBindPrint n = do
t1 <- now'
m <- pure $ fib n -- If `fib` is wrapped in `pure`, and the result `m` is subsequently printed
log (show m)
t2 <- now'
log (show (t2 - t1) <> "ms") -- prints correct ms
pure m
t1 <- now'
m <- pure $ fib n -- If `fib` is wrapped in `pure`, and the result `m` is subsequently printed
log (show m)
t2 <- now'
log (show (t2 - t1) <> "ms") -- prints correct ms
pure m

-- | Fibonacci as an effectful function
fibm :: Int -> Effect Int
fibm 0 = pure 0
fibm 1 = pure 1
fibm n = do
n1 <- fibm (n-1)
n2 <- fibm (n-2)
pure (n1 + n2)
n1 <- fibm (n - 1)
n2 <- fibm (n - 2)
pure (n1 + n2)

-- | Benchmarking the effectful version of Fibonacci, where we monadically bind its result
benchFibmBind :: Int -> Effect Int
benchFibmBind n = do
t1 <- now'
m <- fibm n -- If `fib` itself is defined as an effectful computation `fibm`
t2 <- now'
log (show (t2 - t1) <> "ms") -- prints correct ms (without needing the result `m` to be used beforehand)
pure m
t1 <- now'
m <- fibm n -- If `fib` itself is defined as an effectful computation `fibm`
t2 <- now'
log (show (t2 - t1) <> "ms") -- prints correct ms (without needing the result `m` to be used beforehand)
pure m

benchFibs :: Effect Unit
benchFibs = do
log ("-- | Benchmarking Fibonacci, where we let-bind its result")
_ <- benchFibLetBind 40
log ("-- | Benchmarking Fibonacci, where we let-bind its result and log it")
_ <- benchFibLetBindPrint 40
log ("-- | Benchmarking Fibonacci, where we monadically bind its result (using `pure`)")
_ <- benchFibDoBind 40
log ("-- | Benchmarking Fibonacci, where we monadically bind its result (using `pure`) and log it")
_ <- benchFibDoBindPrint 40
log ("-- | Benchmarking the effectful version of Fibonacci, where we monadically bind its result")
_ <- benchFibmBind 40
pure unit
log ("-- | Benchmarking Fibonacci, where we let-bind its result")
_ <- benchFibLetBind 40
log ("-- | Benchmarking Fibonacci, where we let-bind its result and log it")
_ <- benchFibLetBindPrint 40
log ("-- | Benchmarking Fibonacci, where we monadically bind its result (using `pure`)")
_ <- benchFibDoBind 40
log ("-- | Benchmarking Fibonacci, where we monadically bind its result (using `pure`) and log it")
_ <- benchFibDoBindPrint 40
log ("-- | Benchmarking the effectful version of Fibonacci, where we monadically bind its result")
_ <- benchFibmBind 40
pure unit
3 changes: 1 addition & 2 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import Test.Benchmark.ExperimentEffectNow (benchFibs)

main :: Effect Unit
main = do
benchFibs
if false then run tests else pure unit
if false then run tests else benchFibs

tests :: Array (String × Aff Unit)
tests = concat
Expand Down

0 comments on commit aff4628

Please sign in to comment.