From aff46289d3eb58c1793a49a353042bb20414b823 Mon Sep 17 00:00:00 2001 From: min-nguyen Date: Mon, 2 Oct 2023 11:46:36 +0100 Subject: [PATCH] Added Effect.Now version --- test/Benchmark/ExperimentEffectNow.purs | 88 ++++++++++++------------- test/Benchmark/ExperimentJSDate.purs | 88 ++++++++++++------------- test/Main.purs | 3 +- 3 files changed, 87 insertions(+), 92 deletions(-) diff --git a/test/Benchmark/ExperimentEffectNow.purs b/test/Benchmark/ExperimentEffectNow.purs index 77415f9ff..4edf698c8 100644 --- a/test/Benchmark/ExperimentEffectNow.purs +++ b/test/Benchmark/ExperimentEffectNow.purs @@ -1,6 +1,4 @@ -module Test.Benchmark.ExperimentEffectNow - - where +module Test.Benchmark.ExperimentEffectNow where import Prelude @@ -14,7 +12,7 @@ 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 @@ -22,69 +20,69 @@ 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 diff --git a/test/Benchmark/ExperimentJSDate.purs b/test/Benchmark/ExperimentJSDate.purs index db7a7c6c3..f34e0d927 100644 --- a/test/Benchmark/ExperimentJSDate.purs +++ b/test/Benchmark/ExperimentJSDate.purs @@ -1,6 +1,4 @@ -module Test.Benchmark.JSDate - - where +module Test.Benchmark.JSDate where import Prelude @@ -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 diff --git a/test/Main.purs b/test/Main.purs index 386ee85aa..c935fea9e 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -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