Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will F# perform well when writing math functions for a C# program?

Tags:

performance

c#

f#

Does anyone know how well F# measures with regards to performance, compared to C#. I have a C# raytracer with a lot of vector manipulations, ray-collission algorithms etc. and thought they might be more easily expressed in F#. I'm not asking for how well F# is at expressing math problems, something which has been answered here, but rather if I should expect better or worse performance? As raytracing is very performance intensive, even small cases of poor performance can be a problem in the wrong places.

Edit:

It seems that there are already a lot of questions on the subject that I couldn't find (there are no results if you actually search for anything with the term 'F#'). One good point here was the following answer:

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.

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 is better at optimizing loops using IEnumerables and is better at optimizing computations over value types (e.g. complex arithmetic).

Cheers, Jon Harrop.

like image 456
Morten Christiansen Avatar asked Jan 10 '09 18:01

Morten Christiansen


People also ask

Where is Will Ferrell from?

Ferrell was born on July 16, 1967, in Irvine, California, to Betty Kay (née Overman; born 1940), a teacher who taught at Old Mill School elementary school and Santa Ana College, and Roy Lee Ferrell Jr.


1 Answers

Yes, F# will perform better.

Here are some performance results for a single-thread algorithm implemented in different languages (benchmarking activation function approaches for the neural networks):

C#:

10^7 iterations using Sigmoid1() took 3899,1979 ms
10^7 iterations using Sigmoid2() took 411,4441 ms

Pure C:

10^7 iterations using sigmoid1: 628 ms
10^7 iterations using sigmoid2: 157 ms

F#:

10^7 iterations using sigmoid1: 588.843700 ms
10^7 iterations using sigmoid2: 156.626700 ms

More details

like image 61
Rinat Abdullin Avatar answered Oct 20 '22 18:10

Rinat Abdullin