views:

640

answers:

6

Is F# better than C# in scenarios where you need complete parallelism in parts of an application?

My main language is C# and I am writing an app where the core functionality built on top of the app (C#) has to be and planned to be very parallel.

Am I better of implementing these classes in F#?

+6  A: 

I'd look at the Parallel Extensions that's being developed by Microsoft.

JoshBerke
http://weblogs.asp.net/podwysocki/archive/2009/02/23/adding-parallel-extensions-to-f.aspx
ShuggyCoUk
I thought so too. hoping it is backed into the 2010 release
ShuggyCoUk
+2  A: 

F# is mostly free of side-effects so yes, but you wouldn't want to implement a UI in it. On the flip side, there will be a lot of parallel functionality built in to the next version of C# via LINQ.

You will also have to weigh your training requirements and the fact that F# is not yet RTM and won't be until late this year.

Andrew Robinson
Actually I find F# really useful for GUI code because functional programming makes it easy to run a (lamba) function on the UI thread.
Jon Harrop
+1  A: 

F# makes certain approaches better (e.g. native message support), but you can do it in C# is you minimise (or better, eliminate) mutable state.

As noted in other answers, .NET libraries are going to get a big boost in this way soon with the parallel extensions. These will have, however, exactly the same lack of scalability if you start using locks.

Richard
+3  A: 

If you just need to process sets of data in parallel, the ParallelFX are very handy. They'll take a lot of pain out of doing it manually in C#.

But if the code has a lot of asynchronous code, F#'s async monad makes it vastly easier than can be done in C#. If you're looking at a lot of BeginXXX/EndXXX or the dreaded XXXAsync/XXXCompletedEvent code, then F# will be a major win.

Otherwise, you'll just have the general gains of F# over C#.

MichaelGG
Having to write such code by hand really seines indirekt intothe thinking behind continuation passing style
SealedSun
+1  A: 

Hi, there are two ways of making things parallel, both of them work very well in F#.

  • As Josh already mentioned, there is a Parallel Extensions library. This make it extremely easy to parallelize processing of some data structures - especially immutable data structures that you'll often use in F#. So, combining Paraallel Extensions with F# is the right way to go. This will be discussed in my upcoming real-world F# book, but I'm making the source code available. This is covered in chapter 14 and the source for it should be available soon.

  • For some problems, you can use message-passing concurrency, which is an appealing alternative to shared memory. You can find some information about this in a free chapter from Don Syme's book. It'll be covered in my book as well (Chapter 13 & 16).

  • By combining message-passing concurrency with asynchronous workflows, you can get an excellent way for writing programs that cannot be easily parallelized using Parallel extensions (i.e. they need to share some information & also use asynchronous non-blocking operations (such as downloading from the internet))

Tomas Petricek
+1  A: 

According to this demonstration by Luca Bolognese, the ease of implementing parallelism is one of F#'s stronger points. At around 53 minute mark on, it gets quite impressive. Hope this helps a bit. http://channel9.msdn.com/pdc2008/TL11/

Chang Chung