3

This article spells out some reasons F#'s performance is occasionally better than C#. It says in it's "Firstly" section that only F# generates tail calls.

What exactly does that mean? And why is it a performance boost? This one thing may actually make or break between F# and C# for my chess app, which uses a ton of recursion.

William Barbosa
  • 4,936
  • 2
  • 19
  • 37
Benj Sanders
  • 481
  • 5
  • 15
  • 3
    possible duplicate of [F# and C# 's CLR is same then why is F# faster than C#](http://stackoverflow.com/questions/4857262/f-and-c-sharp-s-clr-is-same-then-why-is-f-faster-than-c-sharp) – Stephen King Aug 27 '15 at 19:35
  • You are way too concerned about the performance difference in C# and F#. Even if there was a performance increase in F# or C#, it doesn't matter because both languages are mature enough to run a chess game at a decent speed, unless someone is running it on an old 486 computer or something. Pick the language you feel most comfortable in and go code your game! – Icemanind Aug 27 '15 at 20:33
  • Initially I just wanted to start learning F#, but as a C# programmer, mostly, I find myself missing several of the OO features of C#. I'm just looking for a reason to stick it out with F# other than it being "different" and "cool" etc. – Benj Sanders Aug 27 '15 at 20:46
  • 1
    F# has almost all of the OO features of C# (notably missing are `protected` and `partial`), but that is not the point. F# is first and foremost a functional language, and the point of using it should be to make use of that, not to do the same as in C# with a different syntax. If you're not prepared to learn doing many things **a lot** different than in C#, you may end up finding F# a waste of time. – TeaDrivenDev Aug 27 '15 at 20:56
  • Ok, since the question is on hold I can't answer it myself but Wikipedia seems to do this nicely: https://en.wikipedia.org/wiki/Tail_call. The answer is that a tail call is a procedure calling itself at the very end of the procedure and instead of calling it again onto the stack (taking memory, resources etc), it reuses this current procedure already in memory. A simple but elegant optimization... this is not a trivial piece of knowledge!!! and makes me want to continue in F#!!! – Benj Sanders Aug 27 '15 at 21:00

1 Answers1

2

Performance will depend more on the way you implement your program than the language. F# may generate IL better for some things while the C# compiler will be better for others. When choosing the languages you should consider other things rather than just performance.

If you're writing your chess program to learn F#, give it a try, it's an awesome language, just don't expect super blazing fast programs just because you're using a functional language.


Edit to answer the new question:

The F# compiler indeed does generate IL that has the tail. op code whareas the C# compiler doesn't. That by itself doesn't make F# faster or more performatic than C#, as you can see in my original answer above, but can indeed make a difference in your specific chess app, since you are stating that recursion is heavily used.

As a side note, the CLR may generate some simpler tail call optimizations during runtime, so for simpler functions in a x64 enviroment, even IL generated by the C# compiler may have tail call optimization.

William Barbosa
  • 4,936
  • 2
  • 19
  • 37
  • Would you mind giving an example of what types of things F#'s IL is better? I know that's pretty general but I'm still learning here. Thank you! – Benj Sanders Aug 27 '15 at 19:41
  • 1
    The difference is in individual syntax constructs, not overall problems. Don't worry about the IL, decide based on the language syntax and capabilities. Come back to the IL when it turns out your F# solution is unacceptably slow; there will almost always be ways to optimize performance individually. – TeaDrivenDev Aug 27 '15 at 20:32