From 3124990875af731611f9d705a3127f92e02808ab Mon Sep 17 00:00:00 2001 From: min-nguyen Date: Mon, 2 Oct 2023 11:29:57 +0100 Subject: [PATCH] Added Effect.Now version --- spago.dhall | 2 + test/Benchmark/ExperimentEffectNow.purs | 90 +++++++++++++++++++ ...{Experiment.purs => ExperimentJSDate.purs} | 2 +- test/Main.purs | 6 +- 4 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 test/Benchmark/ExperimentEffectNow.purs rename test/Benchmark/{Experiment.purs => ExperimentJSDate.purs} (98%) diff --git a/spago.dhall b/spago.dhall index 5cabf820a..9bc33be5c 100644 --- a/spago.dhall +++ b/spago.dhall @@ -11,6 +11,7 @@ You can edit this file as you like. , "bifunctors" , "console" , "control" + , "datetime" , "debug" , "effect" , "either" @@ -28,6 +29,7 @@ You can edit this file as you like. , "maybe" , "newtype" , "nonempty" + , "now" , "numbers" , "ordered-collections" , "parsing" diff --git a/test/Benchmark/ExperimentEffectNow.purs b/test/Benchmark/ExperimentEffectNow.purs new file mode 100644 index 000000000..77415f9ff --- /dev/null +++ b/test/Benchmark/ExperimentEffectNow.purs @@ -0,0 +1,90 @@ +module Test.Benchmark.ExperimentEffectNow + + where + +import Prelude + +import Data.Time (Time, diff) +import Data.Time.Duration (Milliseconds) +import Effect (Effect) +import Effect.Class.Console (log) +import Effect.Now (nowTime) + +-- | Fibonacci as a pure function +fib :: Int -> Int +fib 0 = 0 +fib 1 = 1 +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 + +-- | 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 + +-- | 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 + +-- | 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 + +-- | 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) + +-- | 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 + +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 diff --git a/test/Benchmark/Experiment.purs b/test/Benchmark/ExperimentJSDate.purs similarity index 98% rename from test/Benchmark/Experiment.purs rename to test/Benchmark/ExperimentJSDate.purs index 415a98a0c..db7a7c6c3 100644 --- a/test/Benchmark/Experiment.purs +++ b/test/Benchmark/ExperimentJSDate.purs @@ -1,4 +1,4 @@ -module Test.Benchmark.PureComp +module Test.Benchmark.JSDate where diff --git a/test/Main.purs b/test/Main.purs index a0fa06b43..386ee85aa 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -18,12 +18,12 @@ import Test.Many (bwdMany, linkMany, many, withDatasetMany) import Test.Spec.Mocha (run) import Test.Spec.Specs (bwd_cases, desugar_cases, graphics_cases, linking_cases, misc_cases) import Util (type (×)) -import Test.Benchmark.PureComp (benchFibs) +import Test.Benchmark.ExperimentEffectNow (benchFibs) main :: Effect Unit main = do - if true then benchFibs else pure unit - run tests + benchFibs + if false then run tests else pure unit tests :: Array (String × Aff Unit) tests = concat