Here's a module:
module Foo where
foo ws = let ws' = take 3 $ ws ++ repeat mempty
x = ws' !! 0
y = ws' !! 1
z = ws' !! 2
in [x, y, z]
and here's another one
module Foo where
foo ws = let (x:y:z:_) = take 3 $ ws ++ repeat mempty
in [x, y, z]
Via ghc -O2 -c foo.hs
, they compile down to 8072 and 5288 bytes respectively. Not sure which is best, nor how to test them, but I'd guess they can't behave identically, performance-wise, simply because they are different.
Do the two functions baheave any differently? If not, is the difference in the generated binary due to a missed optimization? Or what?
Here's a module:
module Foo where
foo ws = let ws' = take 3 $ ws ++ repeat mempty
x = ws' !! 0
y = ws' !! 1
z = ws' !! 2
in [x, y, z]
and here's another one
module Foo where
foo ws = let (x:y:z:_) = take 3 $ ws ++ repeat mempty
in [x, y, z]
Via ghc -O2 -c foo.hs
, they compile down to 8072 and 5288 bytes respectively. Not sure which is best, nor how to test them, but I'd guess they can't behave identically, performance-wise, simply because they are different.
Do the two functions baheave any differently? If not, is the difference in the generated binary due to a missed optimization? Or what?
Share Improve this question asked 2 days ago EnlicoEnlico 28.4k8 gold badges67 silver badges147 bronze badges 3 |1 Answer
Reset to default 6Here's one illustrative difference:
-- first module
> foo ("":undefined) !! 0
""
-- second module
> foo ("":undefined) !! 0
"***Exception: Prelude.undefined
!!
3 times, each of which would do some arithmetic and pattern matches (and each call has to start from the beginning of the list again each time). I'd expect the optimiser to inline and remove a lot of the overhead of the!!
version, but I would have no expectation that they would end up compiled to identical bytes. – Ben Commented 2 days ago!!
. – Noughtmare Commented 2 days ago