# C# / F# Performance comparison

See these questions that I asked recently:

Is there any C#/F# performance comparison available on web to show proper usage of new F# language?

This might also be interesting: "Real World Functional Programming"

Examples are in F# and C#, but the theory is fairly generic. From what I've read (pre-release) it is definitely interesting, but so far I think it is making me want to stick more and more with C#, using libraries like Parallel Extensions.

## How can I use functional programming in the real world?

It seems like the book Real World Haskell is just what you're looking for. You can read it free online:

F# has many enormous benefits over C# in the context of mathematical programs:

F# interactive sessions let you run code on-the-fly to obtain results immediately and even visualize them, without having to build and execute a complete application.

F# supports some features that can provide massive performance improvements in the context of mathematics. Most notably, the combination of

`inline`

and higher-order functions allow mathematical code to be elegantly factored without adversely affecting performance. C# cannot express this.F# supports some features that make it possible to implement mathematical concepts far more naturally than can be obtained in C#. For example, tail calls make it much easier to implement recurrence relations simply and reliably. C# cannot express this either.

Mathematical problems often require the use of more sophisticated data structures and algorithms. Expressing complicated solutions is vastly easier with F# compared to C#.

If you would like a case study, I converted an implementation of QR decomposition over `System.Double`

from 2kLOC of C#. The F# was only 100 lines of code, runs over 10× faster and is generalized over the type of number so it works not only on `float32`

, `float`

and `System.Numerics.Complex`

but can even be applied to symbolic matrices to obtain symbolic results!

FWIW, I write books on this subject as well as commercial software.

## How well does F# perform compared to C#?

There is nothing intrinsic that makes one language faster than the other. They both run on the CLR and hence have roughly the same performance characteristics of any language that runs on the CLR. There are features of the respective languages though that do affect performance.

Tail recursion is a great example. If you write an F# app which heavily uses tail recursion, the compiler will attempt to rewrite it as an iterative loop removing the recursion penalty. Additionally F# uses the .tail IL op code in order to ask the CLR to remove the recursion stack penalty where possible.

It's easy to demonstrate this by translating some F# continuation samples to C# and then insert huge data sets. F# will work and C# will eventually crash.

But that is not necessarily a deficiency in C#. C# is not meant to be written with continuations while that is certainly the case in F#. It's in fact not surprising that writing algorithms meant for F# in C# produce not so great results.

Conversely, C# iterators are usually more efficient than F# sequence expressions. The reason is that C# iterators are very efficient state machines vs. F#'s are continuation passing.

In general though, in the abscence of threads, if you use the languages as they are intended to be used they will have similar performance characteristics.