简体   繁体   English

Haskell:功能组合(。)与功能应用程序($)在性能方面有何关系?

[英]Haskell: function composition (.) vs function application ($) when it comes to performance?

In Haskell, when performance matters and using dollars or dots are both valid options, is one better than the other? 在Haskell中,当性能很重要并且使用美元或点都是有效选项时,哪一个比另一个好? Will one result in a performance gain over the other? 一个人会比另一个人获得性能提升吗?

For example, given (foo . bar. baz) value and foo $ bar $ baz value , is one faster than the other? 例如,给定(foo . bar. baz) valuefoo $ bar $ baz value ,是否比另一个更快?

If you are compiling with optimizations ( -O2 ), GHC will nearly certainly inline both . 如果您正在使用优化( -O2 )进行编译,那么GHC几乎肯定会内联两者. and $ and produce foo (bar (baz value)) in both cases (and then optimize it further). $并在两种情况下产生foo (bar (baz value)) (然后进一步优化)。 "Nearly" is just in case; “几乎”就是以防万一; inlining is one of most basic optimizations GHC does, and both . 内联是GHC最基本的优化之一,两者都是. and $ are very simple and inlining them should always be a win, but I may not be thinking of some particular case. $非常简单,内联它们应该永远是一个胜利,但我可能不会考虑某些特定情况。 (One case I can think of when inlining them is harder is when they are partially applied, or passed to a higher-order function, but that's not the example given; or there could be a rewrite rule which fires before inlining and only covers one of these cases.) (我可以想到的一个案例是在内联它们时更难的是它们被部分应用或传递给更高阶函数,但这不是给出的例子;或者可能有一个重写规则在内联之前触发并且只覆盖一个这些案件。)

However, you can always test it in your specific situation using eg Criterion . 但是,您可以使用例如Criterion在特定情况下进行测试。 You can also verify that inlining happened by asking GHC to output Core files . 您还可以通过要求GHC输出Core文件来验证内联是否发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM