简体   繁体   中英

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? 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?

If you are compiling with optimizations ( -O2 ), GHC will nearly certainly inline both . and $ and produce foo (bar (baz value)) in both cases (and then optimize it further). "Nearly" is just in case; inlining is one of most basic optimizations GHC does, and both . 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 . You can also verify that inlining happened by asking GHC to output Core files .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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