caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Optimizing pure-functional streams
@ 2017-07-10 20:26 Alexey Egorov
  2017-07-10 21:29 ` Ivan Gotovchits
  2017-07-11 12:54 ` Simon Cruanes
  0 siblings, 2 replies; 10+ messages in thread
From: Alexey Egorov @ 2017-07-10 20:26 UTC (permalink / raw)
  To: caml users

Hello,

Michael Snoyman recently posted very interesting article about Haskell
streams and Rust iterators (
https://www.fpcomplete.com/blog/2017/07/iterators-streams-rust-haskell
) and I tried to do the same in OCaml.

You can find my code here -
https://gist.github.com/anonymous/127e9116b362d561c5dfa9cce6b453f3

There is four different versions:

1) c_loop.c - plain C loop
2) f_loop.ml - for-loop in OCaml
3) loop.ml - OCaml loop using recursion
4) stream.ml - OCaml loop using streams

Here is the results:

c_loop.c:   0m0.105s
f_loop.ml:  0m0.184s
loop.ml:     0m0.207s
stream.ml: 0m0.605s

It's not suprise that C version is fastest - at least, it uses untagged ints.
What surprises me is that recursion is slower than for-loop - can't
OCaml compile it down to the same imperative loop?

And it's very dissapointing that stream version is 3 times slower than
recursion-based.
The problem, I believe, is that OCaml is unable to optimize
intermediate stream constructors away (as GHC do).

I tried to add inline annotations, which helps a little - is there
something I can do to improve it further?

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2017-07-11 18:55 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-10 20:26 [Caml-list] Optimizing pure-functional streams Alexey Egorov
2017-07-10 21:29 ` Ivan Gotovchits
2017-07-10 22:28   ` Alexey Egorov
2017-07-11 12:40     ` Ivan Gotovchits
2017-07-11 12:54 ` Simon Cruanes
2017-07-11 17:37   ` Ivan Gotovchits
2017-07-11 17:46     ` Yotam Barnoy
2017-07-11 18:04       ` Gabriel Scherer
2017-07-11 18:15         ` Yotam Barnoy
2017-07-11 18:55           ` Ivan Gotovchits

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).