[英]Implicit pattern matching in Haskell
Should one expect performance differences between these two empty
s, or is it merely a matter of stylistic preference? 是否应该预期这两个
empty
之间的性能差异,还是只是风格偏好的问题?
foo list = case list of
[] -> True
(_ : _) -> False
bar list = case list of
(_ : _) -> False
_ -> True
In general you should not expect performance to change predictably between trivial fiddling around with patterns like what you're asking about, and can often expect the generated code to be identical. 一般来说,你不应该期望性能可以通过简单的摆弄与你所询问的模式之间的变化来预测,并且通常可以期望生成的代码是相同的。
But the way to actually check is to look at core and or benchmark with criterion. 但实际检查的方法是使用标准来查看核心和/或基准。 In this case the generated code is the same, and indeed GHC seems to actually combine them:
在这种情况下,生成的代码是相同的,实际上GHC似乎实际上将它们组合在一起:
I compiled the snippet above with 我用上面编译了上面的代码片段
ghc -Wall -O2 -ddump-to-file -ddump-simpl -dsuppress-module-prefixes -dsuppress-uniques -fforce-recomp YourCode.hs
And we see this core: 我们看到了这个核心:
foo :: forall t. [t] -> Bool
[GblId,
Arity=1,
Caf=NoCafRefs,
Str=DmdType <S,1*U>,
Unf=Unf{Src=InlineStable, TopLvl=True, Value=True, ConLike=True,
WorkFree=True, Expandable=True,
Guidance=ALWAYS_IF(arity=1,unsat_ok=True,boring_ok=False)
Tmpl= \ (@ t) (list [Occ=Once!] :: [t]) ->
case list of _ [Occ=Dead] {
[] -> True;
: _ [Occ=Dead] _ [Occ=Dead] -> False
}}]
foo =
\ (@ t) (list :: [t]) ->
case list of _ [Occ=Dead] {
[] -> True;
: ds ds1 -> False
}
-- RHS size: {terms: 1, types: 0, coercions: 0}
bar :: forall t. [t] -> Bool
[GblId,
Arity=1,
Caf=NoCafRefs,
Str=DmdType <S,1*U>,
Unf=Unf{Src=InlineStable, TopLvl=True, Value=True, ConLike=True,
WorkFree=True, Expandable=True,
Guidance=ALWAYS_IF(arity=1,unsat_ok=True,boring_ok=False)
Tmpl= \ (@ t) (list [Occ=Once!] :: [t]) ->
case list of _ [Occ=Dead] {
[] -> True;
: _ [Occ=Dead] _ [Occ=Dead] -> False
}}]
bar = foo
I think the Tmpl
stuff is the original implementation exposed for inlining in other modules, but I'm not certain. 我认为
Tmpl
东西是在其他模块中内联的原始实现,但我不确定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.