views:

4194

answers:

7

I'm guessing that it is not because they both boil down to the same IL

+1  A: 

It will depend. The sort of solutions you will create in F# will often have a very different form to those in C#. It is not uncommon in Functional language code solutions to trade some speed for clarity and succintness.

Mitch Wheat
+13  A: 

You can probably write code in either language that will compile to almost exactly the same CLR bytecode.

The important question is whether idiomatic F# will be more efficient than idiomatic C#.

For example, functional programming languages tend to favor use of immutable datastructures (some, but not F#, even require it). While these datastructures can be made more efficient than their naive implementations (which would involve a lot of cloning), mutable datastructures inherently give more freedom to the programmer, and can therefore be more efficient.

That being said, there is a theoretical argument that immutability is better suited to exploiting multi-core processors and parallelism in general. I'm not aware of any actual research that proves this though.

The bottom line is that there is no simple answer, but generally well-written F# will probably be slower because it places a higher priority on adherence to functional programming ideology, which prioritizes elegance at the expense of efficiency.

sanity
You obviously cannot write code in either language that compiles to the same bytecode because F# generates ILX (e.g. tail calls) and C# does not. F# also makes extensive use of CIL metadata for things that C# does not support (e.g. inlining).
Jon Harrop
Loads of research in purely-functional+parallel=:-) http://www.haskell.org/~simonmar/bib/bib.html
Jared Updike
+2  A: 

I would say that it depends on what "effectiveness" are we talking about. I think that in most cases the final code (IL) is going to be very similar. On the other hand the effectiveness of you, as a developer, might be different. There are certain problems that are more easily described and implemented using a functional language and in those cases the code will be more easily written and maintained using a functional language.

I personally see the power of F# in the fact that because it is built on top of CLR you can mix F# and C# to describe different parts of your applications in different language (you use the one that is more suitable).

PS: The same is true for dynamic languages. I don't think that they are that much better than languages like C# but for certain type of programming you might feel more productive.

David Pokluda
+6  A: 

For a wide range of programs, similar code in the two languages should exhibit similar performance characteristics; it's a goal for F# to have similar perf on like-to-like code comparisons. Simply transliterating code from C# to F# or vice-versa should have almost no effect.

As mentioned by others, some F# data structures and idioms can trade away some perf for other benefits. But it's your choice; F# perfomance methodology embraces imperative programming when it will net you measurable benefits, so if you need to use lots of arrays and for-loops, go for it.

F# does offer a great deal for high-performance computing, notably language features like async workflows (which make it easy to parallelize code or take advantage of multiple CPUs) and a general preference for immutability (which can make multi-threaded code less error-prone).

Finally, though a little outside the scope of the question (which asks for a comparison between the two), don't forget that in the real world you don't always have to choose one or the other. A number of apps have been written with a mix of F# and C#; interop between the two languages is very simple and straightforward, and it often makes sense to author a portion of a large app in one language and another portion in the other, to take advantages of their relative strengths.

Brian
A: 

I would assume the same... Maybe the syntax translates to few more instructions, but it will be negligible...

RWendi

RWendi
+1  A: 

Not right now but certainly F# will perform much faster than C# in some domains later on. The reason is that F# forces you to write code in declarative manner and use immutable types. This is very important because JIT compiler (after some adjustments to it) will be able to utilize multi-core CPUs automatically.

There is also possibility that F# will never become mainstream and somehow C# will inherit F# features in following years making good old C# a new F#.

lubos hasko
+42  A: 

F# provides some performance-related features that can make a difference.

Firstly, the implementation of delegates on .NET is currently quite inefficient and, consequently, F# uses its own FastFunc type for high-performance first-class functions.

Secondly, F# uses .NET metadata to convey inline functions so that they can be exported across APIs and, of course, that can dramatically improve performance in certain circumstances. Moreover, F#'s inlining allows functions passed as arguments to higher-order functions to be completely inlined and type specialized. Our F# for Numerics library makes extensive use of this feature to ensure that per-type functions such as comparison are specialized, giving performance in F# up to 2,350x faster than the equivalent C# (!). In fact, some of our numerical routines are consistently several times faster than vendor-tuned Fortran in libraries like the Intel MKL.

Finally, pattern matching can be extremely laborious to express in C# because the language lacks pattern matching but it is almost impossible to maintain optimized C# code equivalent to many non-trivial pattern matches. In contrast, the F# compiler aggressively optimizes pattern matches during compilation.

Conversely, the C# compiler may still be better at optimizing computations over value types (e.g. complex arithmetic) and has "goto" which can be more efficient than anything currently available in F#.

Cheers, Jon Harrop.

Jon Harrop
Speed and first-class functions: a match made in heaven. <3
Jared Updike