-
Notifications
You must be signed in to change notification settings - Fork 334
Add OCaml benchmark #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add OCaml benchmark #144
Conversation
fibonacci/ocaml/code.ml
Outdated
| let rec aux a b n = | ||
| if n = 0 then a | ||
| else aux b (a + b) (n - 1) | ||
| in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know Ocaml. Does this match the other naive implementation from the other languages?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not use functional languages that much but this program looks exactly like if someone asked me to implement Fibonacci series with naive approach. The only difference is that we go from bottom to top but I cannot imagine how to do it the other way using functional paradigm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a recursive implementation the same as the other but with a difference. It's tail call recursion. So OCaml compiler can optimize in an extreme way such type or recursion.
So, it's recursion, no caching is done, and the good performance comes from the use of tail calls.
But for the sake of comparison, I'll change it to "non tail call"recursion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed. It's important to note that tail call recursion is always preferred (I guess in any language) in OCaml.
botantony
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it works
|
Just adding my 2¢ in passing, but using tail-recursion (TCO) in OCaml is something taught and emphasized from the very beginning. The current code (with TCO removed) is simply never done, it's not an accurate representation of real OCaml code. The original version was correct because, yes, we modify every recursive algorithm to make them tail-recursive whenever possible. That's just how we do it. From the README:
Recursion is one of OCaml's strengths thanks to TCO and real-life code uses TCO as much as possible. It's a fundamental aspect of the language, so let's show it off! EDIT: I think you should also use |
|
Sadly the "tail-recursive" version is |
|
I think this looks compliant, but it is outdated and needs to be rewritten to target the current benchmark runner. See the README about this, and also #371. |
No description provided.