Created
November 8, 2023 23:07
-
-
Save noughtmare/a41f3e822210245d1880d3f397f282aa to your computer and use it in GitHub Desktop.
Why no strict until'?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Prelude hiding (until) | |
import Test.Tasty.Bench | |
{-# NOINLINE until #-} | |
until :: (a -> Bool) -> (a -> a) -> a -> a | |
until p f = go | |
where | |
go x | p x = x | |
| otherwise = go (f x) | |
{-# NOINLINE until' #-} | |
until' :: (a -> Bool) -> (a -> a) -> a -> a | |
until' p f = go | |
where | |
go !x | p x = x | |
| otherwise = go (f x) | |
main = defaultMain | |
[ bench "until" (nf (until (> 1000) (+ 1)) (0 :: Int)) | |
, bench "until'" (nf (until' (> 1000) (+ 1)) (0 :: Int)) | |
] | |
-- Results: | |
-- | |
-- until: OK | |
-- 11.0 μs ± 698 ns, 47 KB allocated, 2 B copied, 6.0 MB peak memory | |
-- until': OK | |
-- 5.68 μs ± 297 ns, 16 KB allocated, 0 B copied, 6.0 MB peak memory |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment